function Parent() {
this.sayAge = function () {
console.log(this.age);
};
}
function Child(firstname) {
this.fname = firstname;
this.age = 40;
this.saySomeThing = function () {
console.log(this.fname);
this.sayAge();
};
}
Child.prototype = new Parent();
let child = new Child("zhang");
child.saySomeThing(); // zhang 40
console.log(child instanceof Parent); // true
function Parent() {}
Parent.prototype.sayAge = function () {
console.log(this.age);
};
function Child(firstname) {
Parent.call(this);
this.fname = firstname;
this.age = 40;
this.saySomeThing = function () {
console.log(this.fname);
this.sayAge();
};
}
Child.prototype = new Parent();
let child = new Child("zhang");
child.saySomeThing(); // zhang 40
function extend(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
return Child;
}
function Parent(lastname) {
this.lastname = lastname;
}
Parent.prototype.sayAge = function () {
console.log(this.age);
};
function Child(firstname, lastname) {
Parent.call(this, lastname); // Mock super
this.firstname = firstname;
this.age = 40;
this.saySomeThing = function () {
console.log(this.lastname, this.firstname);
this.sayAge();
};
}
extend(Child, Parent);
var child = new Child("san", "zhang");
child.saySomeThing(); // zhangsan 40