星空网 > 软件开发 > Java

js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

function ClassA(sColor) {  this.color = sColor;}ClassA.prototype.sayColor = function () {  alert(this.color);};function ClassB(sColor, sName) {
//在 ClassB 构造函数中,用对象冒充继承 ClassA 类的 sColor 属性 ClassA.call(this, sColor); this.name = sName;}//用原型链继承 ClassA 类的方法ClassB.prototype = new ClassA();ClassB.prototype.sayName = function () { alert(this.name);};var objA = new ClassA("blue");var objB = new ClassB("red", "John");objA.sayColor(); //输出 "blue"objB.sayColor(); //输出 "red"objB.sayName(); //输出 "John"

 




原标题:js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok shop小店国家:https://www.goluckyvip.com/tag/83235.html
tiktok免拔ios卡:https://www.goluckyvip.com/tag/83236.html
ios如何上tiktok:https://www.goluckyvip.com/tag/83237.html
tiktok外网加速:https://www.goluckyvip.com/tag/83238.html
tiktok lite下载:https://www.goluckyvip.com/tag/83239.html
自动退款:https://www.goluckyvip.com/tag/8324.html
响沙湾门票套票价格大介绍(省钱攻略必备):https://www.vstour.cn/a/404238.html
上海到镇江茅山旅游攻略 上海到镇江茅山旅游攻略路线:https://www.vstour.cn/a/404239.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流