I am studying John Resig's OOO implementations in JavaScript. The code is like:
(function(){
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
this.Class = function(){};
Class.extend = function(prop) {
var _super = this.prototype;
initializing = true;
var prototype = new this();
initializing = false;
for (var name in prop) {
// ...
}
function Class() {
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.extend = arguments.callee;
return Class;
};
})();
The problem is: why we use initializing here?
I guess that the statement var prototype = new this(); may be async. So when the new ChildClass() is called, the initialization (assignment of the properties to the prototype of ChildClass) may not have been done. But I am pretty not sure whether it is correct or not.
After searching around, I still cannot understand the purpose of using the variable initializing. I found this article has some explanations, but I could not understand: Understanding John Resig's 'Simple JavaScript Inheritance'
Could anybody explain the purpose of it in detailed? Say, give some scenarios where the code fails without the initializing?
Update
Problem solved.
I composed an article to record down the details: Understanding John Resig’s ‘Simple JavaScript Inheritance’