JavaScript call, apply and prototype introduction

Source: Internet
Author: User

1. Preface

Why put these three concepts together to say. The reason is that these will be in the implementation of JS inheritance will need to use

2. The functions of call and Applycall and apply are basically similar, both to perform function and to replace the context of this function with the first parameter. The difference between the two is that call must bring in the parameters of the function, while apply takes a sequence in the second argument.[JavaScript]View PlainCopy
    1. function fn (arg1, arg2,...) {
    2. //Do something
    3. }
    4. FN (arg1, arg2,...);
    5. Fn.call (context, arg1, arg2,...);
    6. Fn.apply (context, [Arg1, arg2,...]);

Explanation of the manual:
===================================
Call method
Invokes one method of an object, replacing the current object with another object.

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

Parameters
Thisobj
Options are available. The object that will be used as the current object.

Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.

Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.

If the Thisobj parameter is not provided, then the Global object is used as the thisobj.
=====================================
[HTML]View PlainCopy
  1. <!--by oscar999 2013-1-17-->
  2. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  6. <title>insert title here</title>
  7. </head>
  8. <body>
  9. <script>
  10. var func=new function () {this.a="func"}
  11. var newfunc=function (x) {
  12. var a="Newfunc";
  13. alert (THIS.A);
  14. alert (x);
  15. }
  16. Newfunc.call (func, "InputParameter1");
  17. /*alert is
  18. * FUNC/INPUTPARAMETER1; Not Newfunc/inputparameter1
  19. */
  20. </Script>
  21. </body>
  22. </html>

As can be seen from the above example, alert (THIS.A), the return is not the value in the current function.


Using call execution can be slightly faster, but with little difference.

3.prototypeJavaScript does not have the concept of "subclass" and "parent class", and there is no distinction between "class" and "instance" (instance), and it relies on a very peculiar "prototype chain" (prototype chain) pattern to implement inheritance.
Prototype is actually a property of the constructor.
All instance objects need to share the properties and methods that are placed inside the object, and those that do not need to be shared are placed inside the constructor.
Once the instance object is created, the properties and methods of the prototype object are automatically referenced. In other words, the properties and methods of an instance object are divided into two types, one local and the other referenced.
[JavaScript]View PlainCopy
    1. function person (name) {
    2. this.name = name;
    3. This.gender = "male";
    4. }
    5. var person1 = New Person ("MM");
    6. var person2 = New Person ("YY");
    7. Person1.gender = "female";
    8. alert (Person2.gender); //Male
    9. </script>

[JavaScript]View PlainCopy
    1. <script>
    2. function person (name) {
    3. this.name = name;
    4. }
    5. Person.prototype.gender = "female";
    6. var person1 = New Person ("MM");
    7. var person2 = New Person ("YY");
    8. alert (Person2.gender); //Male
    9. </script>


Compare the above two examples to know.


4. Reference

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

JavaScript call, apply and prototype introduction

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.