function Person() {
this.name = '123',
this.obj = {
name: "张三"
}
}
Person.prototype.getName = function () {
return this.name
}
function Child() {
}
Child.prototype = new Person();
Child.prototype.constructor = Child
console.log(new Child().getName())
let child = new Child()
child.obj.name = '456'
console.log(new Child().obj.name)
console.log(child)
function Child2() {
Person.apply(this, arguments)
}
console.log(new Child2().name)
try {
console.log(new Child2().getName())
} catch (error) {
}
function Child3() {
Person.apply(this, arguments)
}
Person.prototype.newObj = {
newVal: "2444"
}
Child3.prototype = new Person();
Child3.prototype.constructor = Child3
console.log(new Child3().name)
new Child3().obj.name = "李四"
console.log(new Child3().getName())
console.log(new Child3().newObj.newVal)
new Child3().newObj.newVal = '2333'
console.log(new Child3().newObj.newVal)
function Child4() {
}
var F = function () { };
F.prototype = Person.prototype;
Child4.prototype = new F();
Child4.prototype.constructor = Child4;
function copyExtend(Child, Parent) {
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p) {
c[i] = p[i];
}
}
|