Javascript call, apply and prototype

Source: Internet
Author: User

1. Preface

Why are these three concepts put together. The reason is that these will be used to implement JS inheritance.

2. Call and apply
The call and apply functions are basically the same. They both execute the function and replace the context of the function with the first parameter. The difference between the two is that call must bring the function parameters one by one, while apply only needs to bring the second parameter into a series.
function fn( arg1, arg2,... ){  // do something}fn( arg1, arg2,... );fn.call( context, arg1, arg2,... );fn.apply( context, [ arg1, arg2,... ]);

Manual explanation:
==========================================
Call Method
Call a method of one object to replace the current object with another object.

Call ([thisobj [, arg1 [, arg2 [, [,. argn])

Parameters
Thisobj
Optional. Will be used as the object of the current object.

Arg1, arg2, and argn
Optional. The method parameter sequence will be passed.

Description
The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisobj.

If the thisobj parameter is not provided, the global object is used as thisobj.
============================================

<!--by oscar999 2013-1-17-->  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

From the above example, we can see that alert (this. A) does not return the value in the current function.

The call execution speed is slightly faster, but the difference is not big.

3. Prototype

JavaScript does not have the concepts of "subclass" and "parent class", nor does it distinguish between "class" and "instance, inheritance is implemented by a prototype chain mode.
Prototype is actually an attribute of the constructor.
All attributes and methods that need to be shared by instance objects are placed in this object; those attributes and methods that do not need to be shared are placed in the constructor.
Once an instance object is created, the attributes and methods of the prototype object are automatically referenced. That is to say, the attributes and methods of the Instance Object are divided into two types: local and referenced.

function Person(name){this.name = name;this.gender = "male";}var person1 = new Person("MM");var person2 = new Person("YY");person1.gender = "female";alert(person2.gender);// male</script>

<script>function Person(name){this.name = name;}Person.prototype.gender = "female";var person1 = new Person("MM");var person2 = new Person("YY");alert(person2.gender);// male</script>

Compare the above two examples.

4. Reference

Http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html

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.