1

次のコードは機能しますが、循環参照やメモリ リークが発生するリスクはありますか?

/* 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 is true
4

2 に答える 2

0

チャットで@Raynosによって投稿されたように

@Utilitronは、リークの意味によって異なります。まともなエンジンでのリークまたはIE6でのリークについて話しているのですが、そのコードはv8ではリークしてはなりません。

于 2011-08-19T15:46:01.807 に答える
0

割り当てるとき

core.Container.prototype = new Object();

新しいcore.Containerインスタンスには、コンストラクターとしてObjectが割り当てられます-

これは、コンストラクター関数自体のみを参照します。コンストラクターはFunctionである必要があります。

割当core.Container.prototype.constructor=core.Container

于 2011-08-17T13:33:24.440 に答える