Knockout ViewModel计算垃圾收集

Ada*_*den 5 javascript garbage-collection knockout.js

我一直在尝试在我的应用程序代码中跟踪垃圾收集的任何问题.我已将其剥离为纯粹的淘汰代码,并且根据计算属性的创建方式,似乎存在收集创建对象的问题.

请看以下JS小提琴:http: //jsfiddle.net/SGXJG/

  1. 打开Chrome Profiler.
  2. 拍摄快照
  3. 单击全部制作
  4. 拍摄另一个堆快照
  5. 比较快照
  6. 当正确收集Test1时,Test2和Test3仍保留在内存中.

请参阅以下代码以获取viewmodel:

function ViewModel() {
    this.test1 = null;
    this.test2 = null;
    this.test3 = null;
}
ViewModel.prototype = {
    makeAll: function () {
        this.make1();
        this.make2();
        this.make3();
    },
    make1: function () {
        this.test1 = new Test1();
        this.test1.kill();
        delete this.test1;
    },
    make2: function () {
        this.test2 = new Test2();
        this.test2.kill();
        delete this.test2;
    },
    make3: function () {
        this.test3 = new Test3();
        this.test3.kill();
        delete this.test3;
    },
};
ko.applyBindings(new ViewModel());
Run Code Online (Sandbox Code Playgroud)

以下是三个测试类:

function Test1() {
    var one = this.one = ko.observable();
    var two = this.two = ko.observable();
    this.three = ko.computed(function () {
        return one() && two();
    });
}
Test1.prototype = {
    kill: function () {
        this.three.dispose();
    }
};

function Test2() {
    this.one = ko.observable();
    this.two = ko.observable();
    this.three = ko.computed(function () {
        return this.one() && this.two();
    }, this);
}
Test2.prototype = {
    kill: function () {
        this.three.dispose();
    }
};

function Test3() {
    var self = this;
    self.one = ko.observable();
    self.two = ko.observable();
    self.three = ko.computed(function () {
        return self.one() && self.two();
    });
    self.kill = function () {
        self.three.dispose();
    };
}
Run Code Online (Sandbox Code Playgroud)

不同之处在于Test1'三'计算不使用this或self来引用'one'和'two'可观察属性.有人能解释一下这里发生了什么吗?我想封闭包含对象引用的方式有些东西,但我不明白为什么

希望我没有错过任何东西.如果我有任何回复,请告诉我.