This article mainly introduces the jQuery prototype attributes constructor, selector, length, jquery and prototype Methods size, get, toArray, which are very detailed. If you need them, you can refer to them. First, let's take a look at the prototype attributes and methods defined in jQuery1.7.1?
The first is the constructor attribute.
I believe developers who are familiar with js object orientation are familiar with it. They are used to return functions created by Object Attributes. A simple example is as follows:
function Person(){}; var person=new Person(); alert(person.constructor); //function Person(){}
When writing inheritance, we like to put all the prototype attributes and methods in a separate object literal. This will cause the constructor attribute to be inconsistent with the "actual" direction. For example:
function Person(){ } Person.prototype={ say:function(msg){ alert(msg); } } var person=new Person(); person.say('hello'); alert(person.constructor); //function Object(){[native code]}
At this time, the point will change. Because the literal Object is an instance of the Object, the constructor attribute will execute the Object. To correct this "error", it usually needs to be manually modified back. This is the source code, constructor: jQuery in source code
Selector attributes
The selector attribute is useless for using jquey as the js library. It is mainly used for developing jquery-based plug-ins or transformation. This attribute returns the selector string for obtaining the current jquery object, for example:
var obj=$('p a');console.log(obj.selector);//'p a'
Jquery attributes
This attribute returns the current jQuery version.
console.log($('body').jquery); //1.7.1
Length attribute
This attribute returns the number of elements contained in the jquery object, for example:
console.log ( $('body') .length); //1
The source code of these four attributes is as follows:
constructor: jQuery, // Start with an empty selector selector: "", // The current version of jQuery being used jquery: "1.7.1", // The default length of a jQuery object is 0 length: 0,
Size Method
// The number of elements contained in the matched element set size: function() { return this.length; },
This method returns the length attribute of the jquery object. We recommend that you use the length attribute to reduce unnecessary function call overhead.
ToArray Method
toArray: function() { return slice.call( this, 0 ); },
Restores all DOM elements in the jQuery set to an array.
alert($('li').toArray());[
, ]
First, the slice method is retained in the jQuery constructor, which is the Array prototype method.
// Save a reference to some core methods87 toString = Object.prototype.toString,88 hasOwn = Object.prototype.hasOwnProperty,89 push = Array.prototype.push,90 slice = Array.prototype.slice,91 trim = String.prototype.trim,92 indexOf = Array.prototype.indexOf,
The call method is used to simulate an object. The input parameter 0 indicates that the object is not intercepted. Because this method returns a clean array, which is a pure array, the transformation from a jquery object to a pure array is realized, you can use this method to convert other types of arrays in the future. For example:
JQuery source code analysis-prototype attributes and Methods