标签:function console 原型 prototype script 属性 xiaomi 原型链 构造
原理是将父对象的属性和方法通过prototype进行引用
function people() {
this.flag = true;
this.func = function() {
console.log("this is people func");
}
}
function boy() {
this.sex = "boy";
}
boy.prototype = new people();
var peo1 = new boy();
console.log( peo1.flag ); // true
console.log( peo1.func() ); // this is people func
console.log( peo1.sex ); // boy
缺点:
构造函数继承主要在继承对象中使用 call()、apply() 完成。
function people(name) {
this.name = name || "xiaoming";
}
// 对people进行继承
function boy() {
people.call(this, "wangming");
this.age = 18;
}
var peo1 = new boy();
console.log( peo1.name ); //wangming
console.log( peo1.age ); //18
缺点:
夜已深,明日待续...
标签:function console 原型 prototype script 属性 xiaomi 原型链 构造
原文地址:https://www.cnblogs.com/miku561/p/10487017.html