Thinking about the object-oriented inheritance mechanism call caused by the call () method in JavaScript _javascript skills

Source: Internet
Author: User
Cause:
Today when reading the Snandy of the Great God reading jquery (DOM element), see the ToArray () method, the specific jquery code is as follows:
Copy Code code as follows:

Toarray:function () {
Return Slice.call (this, 0);
},
Get:function (num) {
return num = null?
Return a ' clean ' array
This.toarray ():
Return just the object
(Num < 0 this[this.length + num]: this[num]);
},

See here the call () method, previously also read the manual, said the object is pseudo, used for inheritance. The jquery source is a bit messy, so take this part and put it in a separate file to see the implementation.

But still not quite clear, today decided to study the call. So check the instructions on the MDN, whim, take out my "Sunflower Treasure"-Oxford Dictionary, ready to practice their English level, improve, but also provide some help to have friends (if there is some way out, please forgive the seniors!) )
Call
Summary:
Invokes a function with the given this and arguments
Note: This method is similar to the Apply method syntax, but the difference is that call () accepts the parameter column, and apply () accepts an array of arguments passed to the function
A method of the function class: Version JavaScript 1.3 after edition
Grammar:
Fun.call (thisarg[, arg1[, arg2[, ...)]]
Parameter description:
Thisarg:
Specifies the object for the invocation of Fun (). Note: The This value you see may not be the actual value: If this method is in non-strict mode, null and undefined will be replaced by the global object, and the original value will be encapsulated.
Arg1,arg2,....
The parameters of this object
Describe:
When you call an existing function, you can assign different objects. At this point, this specifies the object that is currently being invoked.
By call, you can write one method at a time and be inherited by another object. Instead of creating the new object yourself, override the method. (That is, the object is impersonating, the following will be an example!) )

The MDN official website above has the example to have a look. In addition, inadvertently on the StackOverflow saw a related issue, see the inside of an answer, suddenly understand the object posing, how to impersonate.
The following section is extracted (click here to read the text):

In JavaScript, methods of the object can be bound to another object at runtime. In ' short, JavaScript allows an object to ' borrow ' the method of another object:
Copy Code code as follows:

Object1 = {
Name: ' Frank ',
Greet:function () {
Alert (' Hello ' +this.name)
}
};
Object2 = {
Name: ' Andy '
};
Note This object2 has no greet method.
But we "borrow" from Object1:
Object1.greet.call (OBJECT2);

The call and apply methods of a function objects (in JavaScript functions are objects as OK) allows you. So in your code for you could say this nodelist is borrowing a array ' s slice method. What does the conversion is the fact this slice returns another array as it ' s result.

The first sentence here is a very graphic one: in JavaScript, the object's method can be bound to another object. To put it simply, JavaScript allows an object to ' borrow ' a method that does not belong to it. "Pseudo" also self-evident, on the above example, Object2 impersonating Object1, to invoke the Object1 method.

PS: Rookie for the first time blog, a little messy, I believe that will slowly improve, to your brother and sister to learn how to write a blog, write a good blog. Also welcome everyone to give me criticism and guidance!

Resources:
Implementation of 1.w3cschool ECMAScript inheritance mechanism

Description of call on 2.MDN

3.stackoverflow

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.