Home >Web Front-end >JS Tutorial >Implement inheritance using call in javascript_javascript skills

Implement inheritance using call in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:20:251045browse

Yesterday, Adan sent me an overloading example in javascript, which feels good. Although I still don’t understand how to implement it, I still posted it.
Implementing setTimeout and passing object objects
See the following code implementation Pass parameters to the function inside



call method Description in JScript Reference: Call a method of an object and replace the current object with another object. call([thisObj[,arg1[, arg2[, [,.argN]]]]]), but there is no example
Instructions in the apply method JScript reference: Apply a method of an object and replace it with another object current object. apply([thisObj[,argArray]])
In fact, the functions of these two are almost the same. What should be noted is that the arg parameter in call(thisObj[,arg1[, arg2[,)) can be a variable. The parameters in apply([thisObj[,argArray]]) are array collections.

This morning I saw another example of using call to implement inheritance. Haha... I also posted it together. This example is relatively simple. Even if it is from the shallower to the deeper

<script> <BR>var _st = window.setTimeout; <BR>window.setTimeout = function(fRef, mDelay) { <BR> if(typeof fRef == 'function'){ <BR> var argu = Array.prototype.slice.call(arguments,2); <BR> var f = (function(){ fRef.apply(null, argu); }); <BR> return _st(f, mDelay); <BR> } <BR> return _st(fRef,mDelay); <BR>} <BR>function test(x){ <BR> alert(x); <BR>} <BR>window.setTimeout(test,1000,'fason'); <BR></script><script> <br><br> function father(){//父类 <br><br> var self=this; //私有变量,子类里不会继承! <br><br> var var_private="private variable"; //私有变量 <br><br> this.var_public="public variable"; //公有变量 <br><br> <br><br> this.author="xling"; <br><br> this.test=function(msg){ //公有方法 <br><br> alert("该方法位于父类 :" + msg + "\n" + self.author); <br><br> } <br><br> <br><br> var test2=function(){ //私有方法,子类不能调用 <br><br> alert("这个方法是父类的私有方法"); <br><br> } <br><br> } <br><br> <br><br> function father2(){ <br><br> this.email="xlingFairy#hotmail.com"; <br><br> } <br><br> <br><br> function suber(){//子类 <br><br> father.call(this);//通过这一句来继承父类(father)类的可见变量及方法(this) <br><br> } <br><br> <br><br> function sun(){ <br><br> suber.call(this); <br><br> father2.call(this);//和上面的一句放在一起,實現多重繼承!爽啊! <br><br> } <br><br> <br><br> var mySuber=new suber(); <br><br> mySuber.test("参数是从子类的实例里传入的"); <br><br> //mySuber.test2(); //这一句会发生错误码,因为test2是父类的私有类 <br><br> alert("父类的私有变量,子类不能读取:" + mySuber.var_private); <br><br> alert("父类的公有变量,子类可以读取" + mySuber.var_public); <br><br> <br><br> var mySun=new sun(); <br><br> mySun.test("这个是从孙子级的实例里传入的参数"); <br><br> alert("父类的私有变量,子类不能读取:" + mySun.var_private); <br><br> alert("父类的公有变量,子类可以读取" + mySun.var_public); <br><br> alert(mySun.email); <br><br></script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn