Здравый смысл в том, что для имитации ООП в JavaScript, мы делаем все, что в терминах функций и прототипы:Определение конструктор производного класса в 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'. –