1对象冒充:

原理是:构造函数使用this关键字给所有属性和方法赋值,因为构造函数只是一个函数,所以可使用ClassA的构造函数成为ClassB的方法,然后调用它,ClassB就会收到ClassA的构造函数中定义的属性和方法

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

代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script language="JavaScript">
function ClassA(sColor)
{
this.color=sColor;
this.sayColor=function(){
alert(this.color);
}
}

function ClassB(sColor,sName)
{
this.newMethod=ClassA;
this.newMethod(sColor);
delete this.newMethod;
this.name=sName;
this.sayName=function()
{
alert(this.name);
}
}

var objA=new ClassA("red");
var objB=new ClassB("blue","li ming");
objA.sayColor();
objB.sayColor();
objB.sayName();
</script>
</body>
</html>

2call方法:

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

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script language="JavaScript">
function sayColor(sprev,snext)
{
alert(sprev+this.color+snext);
}

var obj=new Object();
obj.color="red";
sayColor.call(obj,"The color is "," a very nice color");

//"The color is red a very nice color"
</script>
</body>
</html>

//利用call关键字实现继承
function ClassA(sColor)
{
this.color=sColor;
this.sayColor=function()
{
alert(this.color);
}
}
function ClassB(sColor,sName)
{
ClassA.call(this,sColor);
this.name=sName;
this.sayName=function()
{
alert(this.name);
}
}
//call
var objB=new ClassB("red","zhang");
objB.sayColor();//output "red"
objB.sayName(); //output "zhang"

 

3. apply方法实现继承

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script language="JavaScript">
function ClassA(sColor)
{
this.color=sColor;
this.sayColor=function()
{
alert(this.color);
}
}
function ClassB(sColor,sName)
{
ClassA.apply(this,new Array(sColor));
this.name=sName;
this.sayName=function()
{
alert(this.name);
}
}

var obj = new ClassB("red","li ming");
obj.sayColor();
obj.sayName();
</script>
</body>
</html>

4原型链方式:

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

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

缺点:原型链的弊端在不支持多重继承,记住原型链会用另一类型的对象重写类的prototype属性

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script language="JavaScript">
function ClassA() {
}
ClassA.prototype.color="red";
ClassA.prototype.sayColor=function(){
alert(this.color);
}
function ClassB(){
}
ClassB.prototype=new ClassA();
ClassB.prototype.name="";
ClassB.prototype.sayName=function(){
alert(this.name);
}
//call
var objA=new ClassA();
var objB=new ClassB();
objA.color="blue";
objB.color="yellow";
objB.name="zhang";
objA.sayColor();
objB.sayColor();
objB.sayName();
</script>
</body>
</html>

5混合模式