Use the call in javascript to implement inheritance

Source: Internet
Author: User

Yesterday, Adan sent me a javascript reload example. It feels good. Although I still don't quite understand how to implement it, I posted it.
Implement setTimeout to pass object objects
See the following code to transmit parameters to the function in.
<Script type = "text/javascript">
Var _ st = window. setTimeout;
Window. setTimeout = function (fRef, mDelay ){
If (typeof fRef = 'function '){
Var argu = Array. prototype. slice. call (arguments, 2 );
Var f = (function () {fRef. apply (null, argu );});
Return _ st (f, mDelay );
}
Return _ st (fRef, mDelay );
}
Function test (x ){
Alert (x );
}
Window. setTimeout (test, 1000, 'fason ');
</Script>

Call method description in JScript reference: call a method of an object to replace the current object with another object. Call ([thisObj [, arg1 [, arg2 [, [,. argN]), but no example
Description of the apply method JScript reference: apply a method of an object and replace the current object with another object. Apply ([thisObj [, argArray])
In fact, these two functions are almost the same. Note that the arg parameter in call (thisObj [, arg1 [, arg2 [,) can be a variable, the parameters in apply ([thisObj [, argArray]) are array sets.

This morning I saw another example of using call to implement inheritance. I also posted it together. This example is relatively simple.
<Script language = "javascript" type = "text/javascript">

Function father () {// parent class

Var self = this; // Private variable, which is not inherited in the subclass!

Var var_private = "private variable"; // private variable

This. var_public = "public variable"; // public variable



This. author = "xling ";

This. test = function (msg) {// public Method

Alert ("this method is located in the parent class:" + msg + "\ n" + self. author );

}



Var test2 = function () {// Private method, which cannot be called by subclass

Alert ("this method is a private method of the parent class ");

}

}



Function father2 (){

This. email = "xlingFairy # hotmail.com ";

}



Function suber () {// subclass

Father. call (this); // inherit the visible variables and methods (this) of the parent class through this sentence)

}



Function sun (){

Suber. call (this );

Father2.call (this); // put it together with the above sentence to implement multiple commitments! Awesome!

}



Var mySuber = new suber ();

MySuber. test ("the parameter is passed in from the subclass instance ");

// MySuber. test2 (); // The error code is returned because test2 is the private class of the parent class.

Alert ("private variable of the parent class. The subclass cannot read:" + mySuber. var_private );

Alert ("public variables of the parent class, which can be read by sub-classes" + mySuber. var_public );



Var mySun = new sun ();

MySun. test ("this is a parameter passed in from a grandson-level instance ");

Alert ("private variable of the parent class, which cannot be read by sub-classes:" + mySun. var_private );

Alert ("public variables of the parent class, which can be read by sub-classes" + mySun. var_public );

Alert (mySun. email );

</Script>

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.