復制代碼 代碼如下:
//采用對象冒充的方式實現js繼承
function A(color) {
this.Acolor = color;
this.AshowColor = function() {
document.writeln("Acolor: " + this.Acolor);
}
}
function B(color, name) {
//將newMethod賦值A,調用A的構造函數
this.newMethod = A;
this.newMethod(color);
//然后刪除對A的引用,這樣以后不能調用他
delete this.newMethod;
this.Bname = name;
this.BshowName = function() {
document.writeln("Bname: " + this.Bname);
}
}
var objA = new A("red");
objA.AshowColor();
document.writeln("----------------");
var objB = new B("black", "demo");
objB.AshowColor();
objB.BshowName();
document.writeln("----------------");
新聞熱點
疑難解答
圖片精選