Reputation: 1199
Theorical question
Let's say I hava a variable "test", which is defined as observable:
var test = ko.observable("start");
Some code after, I call some times a function which does:
test = ko.observable("pass n");
The former observable is destroyed each time a new one is associated to test, or it leaks memory? Note: i understand that the second code should do
test("pass n");
I am just curious.
And what about redefining test with var, like in first row?
Upvotes: 0
Views: 125
Reputation: 63729
How this precisely works depends on the actual implementation of JavaScript, specifically the way it does garbage collection. You ask a short and seemingly simple question, when in fact this is a very complicated topic. Check out this related SO question to see the tip of this ice berg.
However, a short short version, AFAIK, if you have in general this kind of code:
var test = ko.observable("abc");
test = ko.observable("xyz");
Then there will probably be no reference to the result of ko.observable("abc")
anymore, and that observable will be subject to garbage collection.
Knockout doesn't have too much to do with that simplified example. Things may get a lot trickier if you have subscriptions flying around, etc. If you want to know more about that you'll need to either ask a way more specific, concrete question, or perhaps in general read up about KO and memory issues. This blog post by RP Niemeyer is a good start.
Upvotes: 2