1.传统形式 --> 原型链
过多的继承了没用的属性
2.借用构造函数
不能继承借用构造函数的原型
每次构造函数都要走一个函数
3.共享原型
不能随便改动自己的原型
4.圣杯模式
// 1. 继承了所有信息
Grand.prototype.lastName = 'zhang';
function Grand() {}var grand = new Grand();Father.prototype = grand;
function Father() {this.name = 'san';
}var father = new Father();Son.prototype = father;
function Son = new Son();// 2. 借用构造函数 每次都要用new
function Person(name, age, sex) {this.name = name;this.age = age;this.sex = sex;
}function Student(name, age, sex, grade) {Person.call(this, name, age, sex);ade = grade;
}var student = new Student('xiaoming', 19, 'male', 2018);// 3. 共用原型
Father.prototypoe.lastName = 'zhang';function Father() {}function Son() {}function inherit(Target, Origin) {Target.prototype = Origin.prototype;
}
inherit(Son, Father); // Son.prototype = Father.prototype
var son = new Son();
console.log(son.lastName); // 'zhang'Son.prototype.sex = 'male';
console.log(son.sex); // 'male'var father = new Father();
console.log(father.sex); // 'male' // 4. 圣杯模式
function inherit(Target, Origin) {function F() {};F.prototype = Origin.prototype;Target.prototype = new F();stuctor = Target; Target.prototype.uber = Origin.prototype; // 超级父级
}// 4.1 圣杯模式
var inherit = (function(){var F = function(){}; // 私有化变量 外部无法访问return function (Target, Origin) {F.prototype = Origin.prototype;Target.prototype = new F();stuctor = Target;Target.prototype.uber = Origin.prototype;}
}())
本文发布于:2024-02-04 22:44:53,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170718075660388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |