js继续经常使用的三种要领,纪录一下,立时要面试了。
以为有效能够帮我点个赞吗?谢谢了。
// 原型链继续
function Parent() {
this.name = '原型链继续';
this.play = [1,2,3];
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child() {
this.type = '原型链继续child';
}
Child.prototype = new Parent();
// 原型链上的原型对象是通用的,转变一个,其他的都邑转变,但我们不想一切对象都转变
var child1 = new Child();
var child2 = new Child();
child1.play.push(4)
console.log(child1.play)
console.log(child2.play)
// 组织函数继续
function Parent() {
this.name = '组织函数继续';
this.play = [1,2,3];
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child() {
Parent.call(this)
this.type = '组织函数继续child';
}
var child1 = new Child();
console.log(child1.getName)
//组织函数继续不会继续原型链上的要领
// 组合继续
// 道理:建立中心对象,中心对象的原型对象是父类的
function Parent() {
this.name = '组合继续';
this.play = [1,2,3];
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child() {
Parent.call(this)
this.type = '组合继续child';
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
//没有这句代码,Child.prototype.constructor会指向Parent
var child = new Child()
console.log(child instanceof Child,child instanceof Parent);
console.log(child.constructor);