您的位置:首页 > 移动开发 > Objective-C

ECMAScript继承机制实现

2016-04-17 14:59 736 查看

继承机制的实现

ECMAScript中没有显示声明class,类是通过function函数来创建的

要用ECMAScript实现继承机制,您可以从要继承的基类入手。所有开发者定义的类都可作为基类。出于安全原因,本地类和宿主类不能作为基类,这样可以防止公用访问编译过的浏览器级的代码,因为这些代码可以被用于恶意攻击。

对象冒充(object masquerading)

构想原始的ECMAScript时,根本没打算设计对象冒充。它是在开发者开始理解函数的工作方式,尤其是如何在函数环境中使用this关键字后才发展出来。

单重继承

超类(基类)定义如下:

function ClassA(sColor) {
this.color = sColor;
this.sayColor = function () {
alert(this.color);
};
}


子类定义如下:

function ClassB(sColor, sName) {
this.newMethod = ClassA;
this.newMethod(sColor);     // ClassA的构造函数被“扩展开”,定义了1个属性和1个方法
delete this.newMethod;      // 继承任务完成,解除对ClassA构造函数的引用

this.name = sName;          // 其他属性定义在后,防止可能被覆盖
this.sayName = function () {
alert(this.name);
};
}


所有新属性和新方法都必须在删除了新方法的代码行后定义。否则,可能会覆盖超类的相关属性和方法。

这种方法了实现的继承,当使用instanceof操作符判断时,ClassB不属于ClassA。如下:

var objB = new ClassB("red", "john");
alert(objB instanceof ClassA);          // 输出 "false"
alert(objB instanceof ClassB);          // 输出 "true"


多重继承

如果存在两个类ClassX和ClassY(并且2个类的构造函数都使用this指针进行定义),ClassZ想继承这两个类,可以使用下面的代码:

function ClassZ() {
this.newMethod = ClassX;
this.newMethod();
delete this.newMethod;

this.newMethod = ClassY;
this.newMethod();
delete this.newMethod;
}


这里存在一个弊端,如果存在两个类ClassX和ClassY具有同名的属性或方法,ClassY具有高优先级。因为它从后面的类继承。除这点小问题之外,用对象冒充实现多重继承机制轻而易举。

call方法

由于这种继承方法的流行,ECMAScript 的第三版为 Function 对象加入了两个方法,即 call() 和 apply()。

call() 方法是与经典的对象冒充方法最相似的方法。它的第一个参数用作 this 的对象。其他参数都直接传递给函数自身。

function sayColor(sPrefix,sSuffix) {
alert(sPrefix + this.color + sSuffix);
};

var obj = new Object();
obj.color = "blue";

sayColor.call(obj, "The color is ", "a very nice color indeed.");


要与继承机制的对象冒充方法一起使用该方法,只需将前三行的赋值、调用和删除代码替换即可:

function ClassB(sColor, sName) {
// this.newMethod = ClassA;
// this.newMethod(color);
// delete this.newMethod;
ClassA.call(this, sColor);      // 使用ClassA的call方法实现继承

this.name = sName;
this.sayName = function () {
alert(this.name);
};
}


apply()方法

apply() 方法有两个参数,用作 this 的对象和要传递给函数的参数的数组。例如:

function sayColor(sPrefix,sSuffix) {
alert(sPrefix + this.color + sSuffix);
};

var obj = new Object();
obj.color = "blue";

sayColor.apply(obj, new Array("The color is ", "a very nice color indeed."));


该方法也用于替换前三行的赋值、调用和删除新方法的代码:

function ClassB(sColor, sName) {
// this.newMethod = ClassA;
// this.newMethod(color);
// delete this.newMethod;
ClassA.apply(this, new Array(sColor));  // 使用ClassA的apply方法实现继承

this.name = sName;
this.sayName = function () {
alert(this.name);
};
}


原型链(prototype chaining)

prototype对象是个模板,要实例化的对象都以这个模板为基础。总而言之,prototype对象的任何属性和方法都被传递给那个类的所有实例。原型链利用这种功能来实现继承机制。

原型链会用另一类型的对象重写类的 prototype 属性

如果用原型方式重定义前面例子中的类,它们将变为下列形式:

function ClassA() {
}

ClassA.prototype.color = "blue";
ClassA.prototype.sayColor = function () {
alert(this.color);
};

function ClassB() {
}

ClassB.prototype = new ClassA();    // 利用原型链实现继承

ClassB.prototype.name = "";         // 其他属性定义在后,防止被覆盖
ClassB.prototype.sayName = function () {
alert(this.name);
};


与对象冒充相似,子类的所有属性和方法都必须出现在 prototype 属性被赋值后,因为在它之前赋值的所有方法都会被删除。

注意:调用ClassA的构造函数,没有给它传递参数。这在原型链中是标准做法。要确保构造函数没有任何参数。

此外,在原型链中,instanceof运算符的运行方式也很独特。对ClassB的所有实例,instanceof为ClassA和ClassB都返回true。例如:

var objB = new ClassB();
alert(objB instanceof ClassA);  // 输出 "true"
alert(objB instanceof ClassB);  // 输出 "true"


在 ECMAScript 的弱类型世界中,这是极其有用的工具,不过使用对象冒充时不能使用它。

原型链的弊端是不支持多重继承。

混合方式

对象冒充的主要问题是必须使用构造函数方式,这不是最好的选择。不过如果使用原型链,就无法使用带参数的构造函数了。开发者如何选择呢?答案很简单,两者都用。

我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。这种方式同样适用于继承机制,用对象冒充继承构造函数的属性,用原型链继承 prototype 对象的方法。用这两种方式重写前面的例子,代码如下:

function ClassA(sColor) {
this.color = sColor;
}

ClassA.prototype.sayColor = function () {
alert(this.color);
};

function ClassB(sColor, sName) {
ClassA.call(this, sColor);          // 使用[对象冒充]继承属性
this.name = sName;
}

ClassB.prototype = new ClassA();        // 使用[原型定义]继承方法

ClassB.prototype.sayName = function () {
alert(this.name);
};


混合方式实现的继承,使用instanceof运算符判断时,ClassB属于ClassA,如下:

var objB = new ClassB("red", "john");
alert(objB instanceof ClassA);          // 输出 "true"
alert(objB instanceof ClassB);          // 输出 "true"


更多请参考:W3School
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息