jquery prototype attributes and Prototyping methods _jquery

Source: Internet
Author: User

First look at the prototype properties and methods defined in jQuery1.7.1.

First, the constructor property.

It is believed that developers familiar with the JS object-oriented section are familiar with the function used to return the creation of object properties, for example:

function person () {};
    var person=new person ();
    alert (person.constructor); function person () {}

When we write inheritance, we like to put all the stereotype properties and methods in a single object literal, which will cause the constructor attribute to point to "actual", 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]}

The point of this time will change because the literal object is an instance of object, the natural constructor property executes object in order to correct this "error" usually need to manually modify back this is the source code, the source code in the constructor:jquery explanation

Selector property

The Selector property is not useful for using Jquey as a JS library it is primarily used to develop jquery-based plug-ins or retrofit uses that return the selector string that gets the current jquery object, for example:

var obj=$ (' div a ');
Console.log (obj.selector);//' Div a '

jquery Properties

This property returns the jquery version currently in use

Console.log ($ (' body '). jquery); 1.7.1

Length Property

This property returns the number of elements contained in the JQuery object, for example:

Console.log ($ (' body '). length); 1

These 4 attribute source code is as follows:

  Constructor:jquery,


  //Start with a empty selector
  selector: "",

  //The current version of JQuery being U Sed
  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;
  },

The method is to return the length property of the JQuery object, which is recommended to use the length property, to reduce the unnecessary function call overhead

ToArray method

Toarray:function () {return
    Slice.call (this, 0);
  },

Restores all DOM elements in the jquery collection to an array.

Alert ($ (' Li '). ToArray ());
[<li id= "foo", <li id= "Bar"]

First of all, the slice method here has been preserved in the constructor of jquery, which is the prototype method of array

Save a reference to some core methods
, Object.prototype.toString, hasown
= object.prototype.h Asownproperty, the
push = Array.prototype.push,
slice = Array.prototype.slice, the
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf,

By using the call method to impersonate an object, passing in parameter 0 means no interception, because this method returns a clean array, a pure array, that transforms from a jquery object to a pure array, and can be converted in the future when it encounters other types of array forms, such as:

<!doctype html>
 
 

So learn Jqeury source code in addition to the use of jquery is helpful, but also learn a lot of JS use skills

Get method

Get the nth element in the matched element set OR//Get the
whole matched element set as a clean array
  get:fu Nction (num) {return
    num = null?

      Return a "clean" array
      this.toarray ():

      //Return just the object
      (num < 0? this[This.length + num) : this[num]);
  

The work of this method is to find one of the elements of the JQuery object and return the JS soundtrack node element object instead of the jquery object, which is different from the EQ method, which takes a parameter and calls the ToArray method to return an array of all the elements if the argument is not saved. If the number is greater than 0, it can be obtained directly by subscript. If it is negative, it is a good way to write some methods to support the subscript of positive numbers by adding to the length. If the write is not a number or exceeds the length of the element contained in the current object, it returns undefined.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.