ECMAScript 实现继承的几种方式
1. 原形鏈
function Father() {
? ? this.fatherName = "licus";
}
function Children() {
this.chidrenName = "king";
}
Children.prototype = new Father();
2.借用構造函數
function Father() {
this.fatherName = "licus";
}
function Children() {
Father.call(this);
}
3.組合繼承
function Father(name){
this.name = name;
}
Father.prototype.sayName = function(){
console.log(this.name);
}
function Children(name, age) {
Father.call(this, name);
this.age = age;
}
Children.prototype = new Father();
4.原型式繼承
function object (o) {
function Instance() {}
Instance.prototype = o;
return new Instance();
}
5.寄生式繼承
function object (o) {
function Instance() {}
Instance.prototype = o;
return new Instance();
}
function createInstance(o) {
var clone = object(o);
clone.Name = "lucis"
return clone;
}
6.寄生組合式繼承
function object(o){
function F() {}
F.prototype = o;
return new F();
}
function inherit Prototype(Children, Father){
var prototype = (Father.prototype);
prototype.constructor = Children;
Children.prototype = prototype;
}
轉載于:https://www.cnblogs.com/LionheartCGJ/p/6227339.html
總結
以上是生活随笔為你收集整理的ECMAScript 实现继承的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: StereoRectify()函数定义及
- 下一篇: 四元素理解