简体   繁体   English

使用YUI库的Javascript

[英]Javascript using the YUI library

Is it possible to pass arguments to a function using YUI's .on("click", )? 是否可以使用YUI的.on(“ click”,)将参数传递给函数? For example here is some code that I'm looking at: 例如,这是我正在查看的一些代码:

function foo1() {
  var curObj = this;

    this.foo2 = function() {
         curObj.test = "foo2";
    }

    this.foo3 = function() {
         curObj.test = "foo3";
    }

  // called by
  this.blah = {};
  var blah = this.blah;
  blah['x'] = new YAHOO.widget.Button(x)
  blah['x'].on("click", foo2)

  blah['y'] = new YAHOO.widget.Button(y)
  blah['y'].on("click", foo3)
}

I'd like to remove some redundancy by doing something like: 我想通过执行以下操作来消除一些冗余:

function setTest(this, foo) {
  this.test = foo;
}

function foo1() {
  var curObj = this;

  // called by
  this.blah = {};
  var blah = this.blah;
  blah['x'] = new YAHOO.widget.Button(x);
  blah['x'].on("click", thisTest("foo2"));

  blah['y'] = new YAHOO.widget.Button(y);
  blah['y'].on("click", thisTest("foo3"));
}

It's my understanding that YUI will pass the "this" object to what ever function is called from .on("click", function). 据我了解,YUI会将“ this”对象传递给从.on(“ click”,function)调用的函数。

Thanks for your help. 谢谢你的帮助。

You can send a single argument, per the API Docs here: http://developer.yahoo.com/yui/docs/YAHOO.util.Element.html#method_on 您可以根据以下API文档发送一个参数: http : //developer.yahoo.com/yui/docs/YAHOO.util.Element.html#method_on

For instance: 例如:

function setTest(this, foo) {
  this.test = foo;
}

function foo1() {
  var curObj = this;

  // called by
  this.blah = {};
  var blah = this.blah;
  blah['x'] = new YAHOO.widget.Button(x);
  blah['x'].on("click", thisTest, "foo2");

  blah['y'] = new YAHOO.widget.Button(y);
  blah['y'].on("click", thisTest, "foo3");
}

If you wanted to pass multiple values, you'd need to either create an Array or Object that contains all the values you want to pass. 如果要传递多个值,则需要创建一个包含要传递的所有值的数组或对象。 This is a limitation in the API. 这是API中的限制。

You can use JavaScript closures to achieve this. 您可以使用JavaScript闭包来实现此目的。 This will also give you more control on the number and type of parameters that you want the event handler to have access to. 这还将使您对希望事件处理程序可以访问的参数的数量和类型进行更多控制。 Also, this method is framework independent. 而且,此方法与框架无关。

eg In the code snippet given in the question, thisTest could perform the closure as follows. 例如,在问题给出的代码段中,thisTest可以按以下方式执行关闭。

var thisTest = function (arg1, arg2) {

    return function () { // handler function

        // arg1 and arg2 will be available inside this function.

        // also any arguments passed to the handler by the caller will be 
        // available without conflicting with arg1 or arg2.

    }
}

Here is a jsFiddle link demonstrating this. 这是一个演示此问题的jsFiddle链接。 http://jsfiddle.net/M98vU/4/ http://jsfiddle.net/M98vU/4/

Two things one must keep in mind here are: 这里必须牢记的两件事是:

  1. Cyclic references caused by attaching event handlers through closures might cause memory leaks in old(ish) browsers. 通过闭包附加事件处理程序引起的循环引用可能会导致旧的(ish)浏览器中的内存泄漏。 Detaching the handlers when not needed or on page unload might be a good idea. 在不需要或不在页面上卸载处理程序时可能是一个好主意。

  2. The fixed / static parameters being passed must be known(determinable) at the time of attaching the handler. 在附加处理程序时,必须知道(确定)要传递的固定/静态参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM