function Parent () {
this.name = "zxx";
}
Parent.prototype.getName = function () {
return this.name
}
function Child () { }
Child.prototype = new Parent();
var child = new Child();
console.log(child.getName())
console.log(child.name)
function Parent () {
this.name = "zx"
}
function Child () {
Parent.call(this)
}
function Parent1 (name) {
this.name = name
}
Parent1.prototype.getName1 = function () {
return this.name
}
Parent1.prototype.sex = "man"
function Child1 (name,age) {
this.age = age;
Parent1.call(this,name);
}
Child1.prototype = new Parent1()
var child1 = new Child1("wd", 23)
console.log(child1.age)
console.log(child1.getName1())
function creatObj(obj) {
function F() {};
F.prototype = obj;
return new F();
}
var person = {
name: "zx",
gf: ["qw","er"]
}
var son1 = creatObj(person)
var son2 = creatObj(person)
son1.gf.push("zx")
console.log(son2.gf)
function Parent2 (name) {
this.name = name;
this.nums = [1,2,3,4,4]
}
Parent2.prototype.sex = "women"
function Child2 (name,age) {
this.age = age
Parent2.call(this,name)
}
var F = function () { }
F.prototype = Parent2.prototype
var f1=new F()
Child2.prototype = f1
var child2 = new Child2("asclin",12)
console.log(child2.sex)
function getBlankObj (obj) {
var F = function (){};
F.prototype = obj.prototype;
return new F();
}
function prototype(son, father) {
son.prototype = getBlankObj(father);
}
function Parent3 (name) {
this.name = name;
this.nums = [1,2,3,4,4]
}
Parent3.prototype.sex = "women"
function Child3 (name,age) {
this.age = age
Parent3.call(this,name)
}
prototype(Child3,Parent3);
var child3 = new Child3("c3",15)
console.log(child3.sex)
原地址链接