Using call in JavaScript to implement inheritance _javascript skills

Source: Internet
Author: User
Yesterday Adam handed me an overloaded example of JavaScript, and it felt good. Although it is still not quite clear. How to do it, but stick it out.
Implementing SetTimeout Object Objects
Look at the following code to achieve the inside function pass parameters
<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>

Description in the call Method JScript reference: A method that invokes an object, replacing the current object with another object. Call ([thisobj[,arg1[, arg2[, [,. argn]]]], but no examples
Apply method A description in the JScript reference: A method that applies an object and replaces the current object with another object. Apply ([Thisobj[,argarray]])
In fact, the two functions are almost identical, and note that the ARG parameter in call (thisobj[,arg1[, arg2[,) can be a variable, and the parameters in apply ([Thisobj[,argarray]]) are set to an array.

This morning I saw an example of using call to implement inheritance. hehe. Also posted. This is a simple example. Even if it's more complicated than it is.
<script language= "javascript" type= "Text/javascript" >

function father () {//Parent class

var self=this; Private variables, subclasses do not inherit!

var var_private= "Private variable"; Private variable

this.var_public= "public variable"; Public variables



This.author= "Xling";

This.test=function (msg) {//Public method

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

}



var test2=function () {//Private method, subclass cannot call

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

}

}



function Father2 () {

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

}



function Suber () {//Subclass

Father.call (this); The visible variables and methods of the parent class (father) class are inherited through this sentence (this)

}



function Sun () {

Suber.call (this);

Father2.call (this);/and the above sentence together, in fact, multiple inheritance! Oh, cool!

}



var mysuber=new suber ();

Mysuber.test ("Arguments are passed in from instances of subclasses");

Mysuber.test2 (); This sentence will have an error code, because Test2 is the private class of the parent class

Alert ("Private variable of parent class, subclass cannot read: + mysuber.var_private");

Alert ("The public variable of the parent class, the subclass can read" + mysuber.var_public);



var mysun=new Sun ();

Mysun.test ("This is the parameter passed in from the grandson-class example");

Alert ("Private variable of parent class, subclass cannot read: + mysun.var_private");

Alert ("The public variable of the parent class, the subclass can read" + 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.