简体   繁体   English

在jquery $({})中包装普通的javascript对象

[英]wrapping plain javascript object in jquery $({})

I have this fragment of code from a book I am reading. 我从一本书中读到了这段代码。 And want to understand what $({}) means and what is its use exactly. 并且想要了解$({})含义以及它的用途。

I tried searching on several search engines and even on SO. 我尝试在几个搜索引擎上搜索,甚至在SO上搜索。 $({}) wasn't a search-friendly term. $({})不是一个搜索友好的术语。

    var Events = {
       bind: function(){
          if ( !this.o ) this.o = $({});
          this.o.bind.apply(this.o, arguments);
       },

       trigger: function(){
          if ( !this.o ) this.o = $({});
          this.o.trigger.apply(this.o, arguments);
       }
    };

I did find a similar question about $([]) but I don't think it is quite the same thing. 我确实找到了关于$([])的类似问题 ,但我不认为它是完全相同的。

You're just wrapping a basic javascript object as a jQuery one. 您只是将一个基本的javascript对象包装为jQuery对象。

From jquery documentation : jquery文档

Working With Plain Objects 使用普通对象

At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: .data(),.prop(),.bind(), .unbind(), .trigger() and .triggerHandler(). 目前,jQuery包装的普通JavaScript对象支持的唯一操作是:.data(),. prop(),. bind(),. unbind(),. turigger()和.triggerHandler()。 The use of .data() (or any method requiring .data()) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789). 在普通对象上使用.data()(或任何需要.data()的方法)将在对象上生成一个名为jQuery {randomNumber}的新属性(例如jQuery123456789)。

 // 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 // test using .data() as summarized above $foo.data('keyName', 'someValue'); console.log($foo); // will now contain a // jQuery{randomNumber} // property // test binding an event name and triggering $foo.bind('eventName', function (){ console.log('eventName was called'); }); $foo.trigger('eventName'); // logs 'eventName was called' 

Should .trigger('eventName') be used, it will search for an 'eventName' property on the object and attempt to execute it after any attached jQuery handlers are executed. 如果使用.trigger('eventName'),它将在对象上搜索'eventName'属性,并在执行任何附加的jQuery处理程序后尝试执行它。 It does not check whether the property is a function or not. 它不会检查属性是否为函数。 To avoid this behavior, .triggerHandler('eventName') should be used instead. 为避免此行为,应改为使用.triggerHandler('eventName')。

 $foo.triggerHandler('eventName'); // also logs 'eventName was called' 

Here's a (not really useful) example : 这是一个(不是很有用)的例子:

​var a =$({});
a.data('b', 3);
console.log(a.data('b')); // this prints 3

If you keep your object created with $({}) , you may use it as callee for data , bind , and so on. 如果保持使用$({})创建对象,则可以将其用作databind被调用者。 This is probably the minimal non DOM keepable jquery object you can make. 这可能是您可以制作的最小的非DOM可保持jquery对象。

this really has more to do with javascript syntax than jQuery. 这真的与javascript语法有关,而不是jQuery。

{} is for objects like so: {}适用于这样的对象:

 //makes an empty object
 var myObject = {}; 

 //makes an object containing 'foo' and 'bar' as 'firstItem' and 'secondItem'
 var myObject = { firstItem : foo, secondItem : bar }; 

[] is for arrays like so: []适用于这样的数组:

 //makes a blank array
 var myArray = [];

 //makes an array containing 'foo' and 'bar' at postions 0 and 1
 var myArray = [foo, bar];

() is for functions (which jQuery generally is). ()用于函数(jQuery通常是)。 This is abit more complicated because it can have multiple meanings. 这更复杂,因为它可以有多种含义。

 //running an existing function
 myFunction();

 //running an anonymous function
 (function(){  
    //doSomething }
 )();

 //running a function with an argument
 myFunction(arg);

jQuery is generally just a function called $ instead of myFunction so... jQuery通常只是一个名为$而不是myFunction的函数,所以......

 //runs jQuery as a function on 'arg'
 $(arg);

The argument you pass jQuery can be almost anything. 你传递jQuery的参数几乎可以是任何东西。 If you pass it a string like '#myDiv' jQuery will use that argument as a selector to get an element from the html. 如果你传递一个像'#myDiv'这样的字符串,jQuery将使用该参数作为选择器从html中获取一个元素。 If you pass it something else like an object or an array, it can still do some things with it like: http://api.jquery.com/jQuery/ as @dystroy said. 如果你把它传递给像对象或数组这样的东西,它仍然可以用它做一些事情: http ://api.jquery.com/jQuery/,如@dystroy所说。

So $({}) is the same as $(myBlankObject) , for example: 所以$({})$(myBlankObject)相同,例如:

var myBlankObject = {};
$(myBlankObject);
//is the same as
$({});

and

var myObjectWithStuff = { firstItem :  foo, secondItem : bar };
$(myObjectWithStuff);
//is the same as
$({ firstItem :  foo, secondItem : bar });

$('selector') works. $('selector')有效。 $({'selector'}) or $(['selector']) does not, because you are not passing jQuery a string, but another data type. $({'selector'})$(['selector'])没有,因为你没有传递jQuery字符串,而是另一种数据类型。

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

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