JS比较思维模型

在这里,要分享的JS中两种思维方式:

1)面向对象风格示例:

function Foo(who){
this.me = who;
}
Foo.prototype.identify = function(){
return "I am"+this.me;
};
function Bar(who){
Foo.call(this,who);
}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.speak = function(){
alert("Hello,"+this.identify()+".");
};
var b1 = new Bar("b1");
var b2 = new Bar("b2");
b1.speak();
b2.speak();

2)对象关联风格示例:

Foo = {
init:function(who){
this.me = who;
},
identify:function(){
return "I am"+this.me;
}
};
Bar = Object.create(Foo);
Bar.speak = function(){
alert("Hello,"+this.identify()+".");
};
var b1 = Object.create(Bar);
b1.init("b1");
var b2 = Object.create(Bar);
b2.init("b2");
b1.speak();
b2.speak();
上一篇:tornado-5.1版本


下一篇:Java基础学习-Eclipse综述和运算符的使用