当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <script>
function Parent(){
this.name = 'mike' ;
}
function Child(){
this.age = 12;
}
Child.prototype = new Parent();
var test = new Child();
alert(test.age);
alert(test.name);
function Brother(){
this.weight = 60;
}
Brother.prototype = new Child();
var brother = new Brother();
alert(brother.name);
alert(brother.age);
</script>
|
以上原型链继承还缺少一环,那就是Object,所有的构造函数都继承自Object。而继承Object是自动完成的,并不需要我们自己手动继承,那么他们的从属关系是怎样的呢?
确定原型和实例的关系
可以通过两种方式来确定原型和实例之间的关系。操作符instanceof和isPrototypeof()方法:
1 2 3 4 | alert(brother instanceof Object)
alert(test instanceof Brother);
alert(brother instanceof Child);
alert(brother instanceof Parent);
|
只要是原型链中出现过的原型,都可以说是该原型链派生的实例的原型,因此,isPrototypeof()方法也会返回true。
相关学习推荐:js视频教程
以上就是怎么体现js继承关系的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
变量必须以什么开头javascript
分享几个实用的单行 js 代码
html怎么引入js文件?
js 中排名前十的报错如何避免
js怎么替换css样式
javascript与html的结合方法详解
js中的typeof和instanceof和===的区别
一起看看ecmascript和javascript的区别
js如何实现盒子拖拽效果?(附代码)
网页中写js有什么作用
更多相关阅读请进入《js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 怎么体现js继承关系