< 2007年4月 >
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

I think I can fly , and flying like a bird ！

free web counter

# 随笔档案

•

• 积分 - 183530
• 排名 - 305

### 评论排行榜

1.对象冒充
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function () {
};
}

function ClassB(sColor, sName) {
this.newMethod = ClassA;
this.newMethod(sColor);
delete this.newMethod;

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

var objA = new ClassA("red");
var objB = new ClassB("blue", "Nicholas");
objA.sayColor();
objB.sayColor();
objB.sayName();

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

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

2.call()方法
call()方法是与经典的对象冒充方法最相似的方法，它的第一个参数用作this的对象，其他参数都直接传递函数自身。
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function () {
};
}

function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(color);
//delete this.newMethod;
ClassA.call(this, sColor);

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

var objA = new ClassA("red");
var objB = new ClassB("blue", "Nicholas");
objA.sayColor();
objB.sayColor();
objB.sayName();

3.apply()方法
apply()方法有两个参数，用作this的对象和要传递参数的数组。例如：
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function () {
};
}

function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(color);
//delete this.newMethod;
ClassA.apply(this, arguments);

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

var objA = new ClassA("red");
var objB = new ClassB("blue", "Nicholas");
objA.sayColor();
objB.sayColor();
objB.sayName();

4.原型链
function ClassA() {
}

ClassA.prototype.color = "red";
ClassA.prototype.sayColor = function () {
};

function ClassB() {
}

ClassB.prototype = new ClassA();

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

var objA = new ClassA();
var objB = new ClassB();
objA.color = "red";
objB.color = "blue";
objB.name = "Nicholas";
objA.sayColor();
objB.sayColor();
objB.sayName();

5.混合方式

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

ClassA.prototype.sayColor = function () {
};

function ClassB(sColor, sName) {
ClassA.call(this, sColor);
this.name = sName;
}

ClassB.prototype = new ClassA();

ClassB.prototype.sayName = function () {
};

var objA = new ClassA("red");
var objB = new ClassB("blue", "Nicholas");
objA.sayColor();
objB.sayColor();
objB.sayName();

1.zlnherit库

zInherit库给Object类添加了两个方法，inheritFrom()和instanceOf()
ClassB.prototype.inheritFrom(ClassA);
CalssB.instanceOf(ClassA);

2.xbObjects库

_classes.registerClass("Subclass_Name","Superclass_Name");
这里，子类和超类名都以字符串形式传进来，而不是指向它们的构造函数的指针。这个调用必须放在指定子类的构造构函数前。

_classes.registerClass("ClassA");
function ClassA(color){
_classes.defineClass("ClassA",prototypeFunction);

function prototypeFunction(){
//...
}
}

_classes.registerClass("ClassA");
function ClassA(color){
_classes.defineClass("ClassA",prototypeFunction);
this.init(sColor);
function prototypeFunction(){
ClassA.prototype.init = function(sColor){
this.parentMethod("init");
this.color = sColor;
}
}
}

_classes.registerClass("ClassA");
function ClassA(color){
_classes.defineClass("ClassA",prototypeFunction);
this.init(sColor);
function prototypeFunction(){
ClassA.prototype.init = function(sColor){
this.parentMethod("init");
this.color = sColor;
}
ClassA.prototype.sayColor = function(){
}
}
}

var objA = new ClassA("red");
objA.sayColor();      //outputs "res"
posted on 2007-04-19 15:41 cresposhi 阅读(1175) 评论(1)  编辑  收藏

FeedBack:
# re: javascript学习笔记(二)－－继承【转载自好朋友祝子的blog】  2008-03-06 12:01 kirari_wxy

 只有注册用户登录后才能发表评论。 网站导航: