0
传统观念认为在JavaScript中模拟OOP,我们的职能和原型方面做的一切:在Javascript中定义构造函数派生类
var fooObject = function() {
//Variables should be defined in the constructor function itself,
//rather than the prototype so that each object will have its own variables
this.someProperty = 5; //Default value
};
//Functions should be defined on the prototype chain so that each object does not
//have its own, separate function methods attached to it for performing the same
//tasks.
fooObject.prototype.doFoo = function() {
//Foo
}
现在,创建一个派生类,我们这样做:
var derivedFromFoo = new foo();
但是如果我们想要做一些其他的东西在我们的构造函数派生的对象会发生什么?像设置其他属性?我们可以这样做
var derivedFromFoo = function() {
this = new foo();
};
FWIW,你永远不能指定'this'。 – 2014-09-28 02:45:16