The call method in JavaScript

Source: Internet
Author: User

The method is applied to the function object (that is, the function itself or a function instance created through the New keyword)

The first use of this method is to invoke a method of an object (very dry, isn't it?) Official explanations are often non-speaking, see the example below):

function Add (b) {        document.write (a + b);} function Sub (A, b) {        document.write (a);} Add.call (sub,3,1);

Page output results are: 4
So here's a summary: The call method can be called by function A, called after a number of parameters, the first parameter is function B, the second to third parameter is the function to execute the argument, the call method executes the result is function A to replace function B function body content, So the above example performs the added operation instead of subtracting it.
What is this feature for? Does it feel good to waste? Don't panic, it's interesting down there.

function Class1 () {        this.name = "Sam";        This.showname = function () {                alert (this.name);                }} function Class2 () {        this.name = "Jack";        }

Then create an instance of Class1

var C1 = new Class1 (); C1.showname ();

Page output "Sam", no problem
And then create an instance of Class2

var C2 = new Class2 (); C1.showName.call (C2);

Page Output "Jack"
The result seems a bit scary, Class2 instance can be called by a call method Class1 private method, but after the thriller did not feel that this function and some JavaScript is not in a very important function in other programming languages like?
Then look down.

function Class1 () {        this.sayhi = function (TXT) {                alert (TXT)}        } function Class2 () {        class1.call (this);        }

Then create an instance of Class1

var C1 = new Class1 (); C1.sayhi ("Hello,jack");

Page output "Hello,jack", no problem
And then create an instance of Class2

var C2 = new Class2 (); C2.sayhi ("Hello,sam");

Page Output "Hello,sam"
Code here, "inherit" The word has surfaced, in Class2 added a sentence class1.call (this) so that Class2 inherit the Class1 properties and methods, JavaScript is the call method to simulate the concept of inheritance
Class1.call (This): this in this code refers to the current object is Class1 itself, Class1.call is to use the Class1 object instead of the current Class2 object (some articles on the web called "Object Impersonation"), So the Class2 instance has the properties and methods of Class1.
What needs to be explained is that I think the word "replace" is not accurate, if CLASS2 also has its own properties and methods, then these properties and methods must have been there, if the "replacement" will make people think of the properties and methods of Class2 itself to kill, but temporarily unexpectedly more appropriate description, Just remember that.
This is the basic inheritance, can the inheritance in JavaScript be a little bit cooler? Look at the code below

function Class1 () {        this.sayhi = function () {                alert ("Hello,world");                }        } function Class2 () {        class1.call (this);        } function Class3 () {        class2.call (this);        }

Then create an instance of CLASS3

var C3 = new Class3 (); C3.sayhi ();

The page actually output "Hello,world"!
Thus, through the call method, JavaScript also realizes the long-lost multi-inheritance in the lake.

The call method in JavaScript

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.