<script language="javascript">
function classA(sColor){
this.color = sColor;
this.sayColor = function(){
alert(this.color);
}
}
function classB(sColor,name){
this.newMethod = classA;
this.newMethod(sColor);
delete this.newMethod;
this.name = name;
this.sayName = function(){
alert(this.name);
}
}
var a = new classA("red");
var b = new classB("blue","bbb");
a.sayColor();
b.sayColor();
b.sayName();
</script>
关键字this引用的是构造函数当前创建的对象,而作为某个方法被使用的时候,this则是当前调用该方法的对象,那么当上面调用new classA时,其中的this作为新创建的classA的对象,而当在classB中使用的时候,是new classB所产生的对象,这样也就达到了对象冒充方式来使classB继承了A的color属性和sayColor方法
分享到:
相关推荐
JavaScript中this的指向还没搞明白?来这看看 你就懂啦~
Javascript 中 this指向
JavaScript 中的 this 关键字是一个非常重要的概念,它经常会使开发者感到困 惑。通常来说,this 的值是在函数被调用时确定的,其值取决于函数被调用的方 式。本文将介绍 JavaScript 中 this 的用法,从而帮助开发者...
JavaScript程序设计javascript中this的指向问题共6页.pdf.zip
this指触发事件的对象,接下来为大家分享下javascript中onclick(this)的用法,感兴趣的朋友可以参考下哈,希望对你有所帮助
NULL 博文链接:https://wv1124.iteye.com/blog/505231
JavaScript中this指向.pdf
JavaScript中this指向.docx
JavaScript中this的使用,简单理解,快速学会
复习JavaScript中this指向及绑定
详解Javascript 中的this指针
高手详解javascript中的this指针
探寻JavaScript中this指针指向_.docx
Javascript的this用法
详解JavaScript中this关键字的用法_.docx
深化理解Javascript中this的作用域_.docx
JavaScript 中的this 总是让人迷惑,应该是js 众所周知的坑之一。 个人也觉得js 中的this 不是一个好的设计,由于this 晚绑定的特性,它可以是全局对象, 当前对象,或者…有人甚至因为坑大而不用this。 其实如果...
JavaScript中的this指向.md
详解Javascript 中的this指针
详解javascript中的this对象.docx