开发者

Will setting this.constuctor = this cause a circular reference/memory leak

The following code works, but am I running the risk of causing circular reference or a memory leak?

/* core package */
var core = function() {
    // Throw an error, the core package cannot be instantiated.
    throw new Error('A package cannot be instantiated.');
};

core.Container = function (properties){
    this.constructor = this;
    this.id = null;
    if ('id' in properties)
        this.id = properties['id'];
    else 
        throw new Error('A container must have an id.');
}
core.Container.prototype = new Object();

var container = new core.Container({'id': 'container'});
alert(container instanceof core.Container); // Result i开发者_JAVA技巧s true


when you assign

core.Container.prototype = new Object();

new core.Container instances are assigned Object as their constructor-

this only refers to the constructor function itself, whose constructor should be Function.

Assign core.Container.prototype.constructor=core.Container


As posted by @Raynos in chat

@Utilitron depends what you mean by leaks are we talking leaks in decent engines or leaks in IE6 That code shouldnt leak in v8

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜