简体   繁体   English

$()和$之间有什么区别?

[英]What is the difference between $() and $.?

I would like to figure out the difference between using $() and $. 我想弄清楚使用$()$.之间的区别$. from other developers. 来自其他开发者。

As far as my understanding goes, $() refers to objects within the DOM , but I am not 100% clear as to how the $. 据我所知, $()引用了DOM对象,但我并不是100%清楚如何$. works. 作品。 I have used the $. 我用过$. format before, but never understood how it works. 格式之前,但从未理解它是如何工作的。

For example: 例如:

$.each(element, function() {}); or $.fn etc... $.fn等...

It would be nice to shed some light and clarity on this topic. 在这个主题上阐明一些亮点和清晰度会很不错。

$ is an identifier. $是一个标识符。 It is used as a variable. 它用作变量。 It has a function assigned to it. 它具有分配给它的功能。

Putting () after a function will call it. 在函数之后放置()将调用它。 The function jQuery assigns to it does lots of different things depending on what sort of arguments you pass to it. jQuery赋予它的函数会做很多不同的事情,具体取决于你传递给它的参数类型。 (It is horribly overloaded). (这是可怕的超载)。 (eg if you pass it a function, it will call that function when the document ready event fires. If you pass it a string of HTML, it will create a DOM representation of that HTML and wrap it in a jQuery object. If you pass it a DOM node, it will wrap that node in a jQuery object. If you pass it a CSS selector, it will search the document for matching DOM nodes and wrap them with a jQuery object). (例如,如果你传递一个函数,它将在文件就绪事件触发时调用该函数。如果你传递一个HTML字符串,它将创建该HTML的DOM表示并将其包装在jQuery对象中。如果你通过它是一个DOM节点,它将该节点包装在一个jQuery对象中。如果你传递一个CSS选择器,它将在文档中搜索匹配的DOM节点并用jQuery对象包装它们。

In JavaScript, functions are objects. 在JavaScript中,函数是对象。 Objects can have properties. 对象可以具有属性。 You can access a property on an object via $.name_of_property . 您可以通过$.name_of_property访问对象上的属性。 Those properties can also have functions (or other objects) assigned to them. 这些属性还可以分配给它们的函数(或其他对象)。

$() creates an jQuery instance object that holds some list of elements. $()创建一个包含一些元素列表的jQuery实例对象。 Methods 方法

$ is the one-and-only jQuery object, that has some non-instance-specific methods. $是唯一的jQuery对象,它有一些非特定于实例的方法。 (It may be helpful to think of them as "static" methods, to borrow terminology from other languages). (将它们视为“静态”方法,借用其他语言的术语可能会有所帮助)。

This one-and-only jQuery object, identified by $ , is a function (which is why it is callable as $() ). 这个由$标识的唯一jQuery对象是一个函数(这就是为什么它可以调用为$() )。 However, functions in JavaScript are objects , so they can have properties and member functions themselves. 但是,JavaScript 中的函数是对象 ,因此它们本身可以具有属性和成员函数。

Basically, $() is a constructor function - you pass it a DOM selector, and it returns a jQuery object. 基本上, $()是一个构造函数 - 你传递一个DOM选择器,它返回一个jQuery对象。

On the other hand, $.each $().each or $. 另一方面, $.each $().each$. $(). is a prototype function of jQuery. 是jQuery的原型函数。

Edit: 编辑:

I stand corrected by @apsillers: $().each is a prototype functions, but $. 我支持@apsillers: $().each都是原型函数,但$. (ie $.each ) are defiend directly on jQuery , or $ . (即$.each )直接在jQuery$

Code sample from jQuery to back up my claim: 来自jQuery的代码示例来支持我的声明:


jQuery.fn = jQuery.prototype = {
   .....
   .....
   .....
   // Execute a callback for every element in the matched set.
   // (You can seed the arguments with an array of args, but this is
   // only used internally.)
   each: function( callback, args ) {
      return jQuery.each( this, callback, args );
   },
   ....
   ....
};

As you can see, $().each is a prototype function. 如您所见, $().each都是prototype函数。 jQuery.each is an internal method. jQuery.each是一个内部方法。 defined directly on jQuery . 直接在jQuery上定义。

Taken from jQuery Source Code . 取自jQuery源代码

$ is just a variable with a short name that points to the same thing as jQuery . $只是一个带有短名称的变量,指向与jQuery相同的东西。 $() is the same as jQuery() (calling jQuery as a function), and $.foo is the same as jQuery.foo . $()jQuery() (将jQuery作为函数调用)相同, $.foojQuery.foo相同。 jQuery.foo accesses the foo property of the jQuery object – function objects can have other properties too. jQuery.foo访问jQuery对象的foo属性 - 函数对象也可以有其他属性。

$ is the jQuery object while $() is an evaluation of that object (which is also a function). $是jQuery对象,而$()是对该对象的评估(也是一个函数)。

The meaning of $() is (from http://api.jquery.com/jQuery/ ): $()的含义是(来自http://api.jquery.com/jQuery/ ):

Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string 返回根据传递的参数在DOM中找到的匹配元素的集合,或者通过传递HTML字符串创建的匹配元素的集合

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

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