JQuery prototype attributes and prototype attributes

Source: Internet
Author: User

JQuery prototype attributes and prototype attributes

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=$('div a');console.log(obj.selector);//'div 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());[<li id="foo">, <li id="bar">]

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:

<! Doctype html> 

Therefore, learning jqeury Source Code not only helps jquery, but also learns many js usage skills.

Get Method

// Get the Nth element in the matched element set OR// Get the whole matched element set as a clean array  get: function( num ) {    return num == null ?      // Return a 'clean' array      this.toArray() :      // Return just the object      ( num < 0 ? this[ this.length + num ] : this[ num ] );  },

This method is used to find one of the elements in the element array of the jquery object and return the node Element Object of the js source sound instead of the jquery object. This is different from the eq method, this method accepts a parameter. If the parameter is not saved, the toArray method is called to return an array containing all elements, if it is a number greater than 0, it can be obtained directly through the lower mark method. If it is a negative number, it is obtained by adding the number to the length. To write some methods, we need to support a good method of adding positive and negative numbers to the lower mark. If it is not a number or exceeds the length of the element contained in the current object, undefined is returned.

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.