jQuery:$({})做了什么,jQuery apply方法是什么?

时间:2012-01-24 04:35:00

标签: jquery

我知道$()是jQuery函数......但$({})引用/做什么?我在https://gist.github.com/705311

中查看Ben Alman的代码
(function(jQuery){
  var o = jQuery({});
  jQuery.each({ 
    "subscribe" : "bind", 
    "unsubscribe" : "unbind", 
    "publish" : "trigger" 
  }, function ( fn, api ) {
    jQuery[ fn ] = function() {
      o[ api ].apply( o, arguments );
    };
  });
})(jQuery);

我正试图弄清楚这段代码是如何完成的。有人可以分解吗?另外,什么是jQuery.apply()方法?我没有在jQuery文档中看到它 - 我只能找到一个4岁的jQuery apply()插件,我怀疑它现在是核心。

https://gist.github.com/661855有一个更新,更简单的版本,但我更好奇这个代码的0.X版本是什么/如何工作。

.. ..编辑 我意识到$({})是一个用jQuery包装的空对象。问题是,为什么这样做,以及他最终如何从这一小段代码创建$ .subscribe(),$ .unsubscribe()和$ .publish(),尤其是o[ api ].apply( o, arguments );位?

4 个答案:

答案 0 :(得分:4)

此代码创建一个用于观察者模式的对象。这是一行一行的解释:

// opening a self-executing anonymous function closure
// assigning global variable jQuery (see last line)
// to local argument jQuery (usually this would be $)
(function(jQuery){

  // creating a jQuery selector object to access the available methods
  // typeof $.unbind == 'undefined'
  // typeof $({}).unbind == 'function'
  // And more importantly this new object will be the container
  // for our observer pattern (see below)
  var o = jQuery({});

  // iterating over an object with new method keys to old method values
  // creating syntactic sugar for an observer pattern (footnote 1)
  jQuery.each({ 
    "subscribe" : "bind", 
    "unsubscribe" : "unbind", 
    "publish" : "trigger" 

  // here fn will be the the new method name and api will be the old one
  }, function ( fn, api ) {

    // a property of the jQuery namespace is created with the new method name
    jQuery[ fn ] = function() {

      // which calls the old method applying given arguments (footnote 2)
      // to our new observer container object
      o[ api ].apply( o, arguments );

    };
  });
})(jQuery);

脚注1:见http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx

脚注2:见http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx

答案 1 :(得分:0)

http://api.jquery.com/jQuery/

jQuery( object )
  

object要包装在jQuery对象中的普通对象。

因此,这只是返回具有jQuery对象方法和属性的对象的一种方法 - 不使用任何其他对象或元素作为上下文。 ({}只是一个没有任何附加属性的新JavaScript对象。)

api是上述字符串之一,例如subscribeo[ api ]会从subscribe返回o函数。此函数(与任何函数一样)具有apply函数 - 详见https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply

答案 2 :(得分:0)

o = jQuery({})初始化一个emply JQuery对象.jQuery [fn] = function(){           o [api] .apply(o,arguments);链接订阅,取消订阅,发布自定义事件以绑定,取消绑定和触发。我认为代码将这些方法挂钩绑定,取消绑定和触发。只需在firebug中看到对象转储。

(function(jQuery){
  var o = jQuery({});
  jQuery.each({ 
    "subscribe" : "bind", 
    "unsubscribe" : "unbind", 
    "publish" : "trigger" 
  }, function ( fn, api ) {
    jQuery[ fn ] = function() {
      o[ api ].apply( o, arguments );

    };
  });
 //watch o in firebug
   console.log(o);
})(jQuery);

答案 3 :(得分:0)

jQuery({});

这里,创建一个jQuery对象,里面有空白对象。将jQuery对象传递给$()函数时,会创建该对象的克隆。这个新的jQuery对象引用与初始DOM元素相同的DOM元素。

以下示例非常明确。

克隆jQuery对象

// define a plain object
var foo = {foo:'bar', hello:'world'};

// wrap this with jQuery
var $foo = $(foo);

// test accessing property values
var test1 = $foo.prop('foo'); // bar

// test setting property values
$foo.prop('foo', 'foobar');
var test2 = $foo.prop('foo'); // foobar