0

I have a simple question about cycles references in javascript.

Is this a cycle reference?

var theThing=null;

theThing = {
longStr: new Array(1000000).join('*'),
someMethod: function () {
  console.log(someMessage);
}

someMethod context captures theThing variable right?

Example 2:

var theThing = null;
var replaceThing = function () {
var originalThing = theThing;

theThing = {
longStr: new Array(1000000).join('*'),
someMethod: function () {
  console.log(someMessage);
}
};
};
setInterval(replaceThing, 1000);

In example 2, is there an cycle reference? SomeMethod references originalThing which is the Thing.

Nick
  • 2,818
  • 5
  • 42
  • 60

2 Answers2

0

You mean Scope?

If you call theTing.someMethod(), then theThing is in scope and accessable in the method.

Furthermore, there is no someMethod context. When we talk about context in JavaScript, we refer to this. And within theThing.someMethod you have indeed access to the context (this) of theThing.

var foo = "bar";

var obj = {
    name: "Lukas",
    method: function() {
        // scoped foo variable
        console.log(foo);
        // scoped obj variable
        console.log(obj.name);
        // context
        console.log(this.name);
    }
}

obj.method();
Luke
  • 8,235
  • 3
  • 22
  • 36
  • The `theThing` variable is always in scope, regardless how you call the function. If you meant the `this` value of the call, that's not "scope". – Bergi Jun 04 '18 at 11:44
  • please can you tell me where I said, that depending how you call the function, the scope is different? And also, where did I say that "this" is scope? I said `this` is context :) – Luke Jun 04 '18 at 12:19
  • "*If you call `theTing.someMethod()`, then `theThing` …*" sounded to me like it depended on how it was called. Regarding the `this` keyword, sorry I didn't read your post carefully enough, looks fine. – Bergi Jun 04 '18 at 12:31
  • Yeah, but if he would copy the someMethod function to a completly different place, it would still have theThing scoped, no matter what. I was just saying, that the var "theThing", as it is hoisted, wiill be available in the scope. – Luke Jun 04 '18 at 12:37
0

In example 1, someMethod context captures theThing variable right?

It could capture it but it doesn't - you reference someMessage in the console.log call not theThing. But since both are global variables, they're closed over by the function anyway.

In example 2, is there an cycle reference? SomeMethod references originalThing which is the Thing.

Again, no, someMethod only closes over someMessage (and console). replaceThing captures theThing, though, that's a genuine example of a closure. Nothing references originalThing, that's a local variable which is initialised but then used nowhere and will get thrown away as soon as possible.

There's no specific cyclic reference other than the basic global environment referencing thingThing referencing someMethod closing over the global environment.

I have a simple question about [memory-leaks]

Nothing in here constitutes a memory leak.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/ Section 4 Closures. "Even though unused is never used, someMethod can be used through theThing. And as someMethod shares the closure scope with unused". Because someMethod doesnt actually use the unsed or in this case the originalThing doesnt mean that it does not reference it. – Nick Jun 04 '18 at 12:18
  • @Nick in the example in your question, there is no `unused`, and nothing refers to `originalThing`. – Bergi Jun 04 '18 at 12:34
  • @Berg- If we would put the originalThing inside someMethod then would it be a cycle reference? – Nick Jun 04 '18 at 12:38
  • @Nick No, why would that be a cyclic reference? It's a chain of large objects referencing each other without being actually used, so it probably creates a memory leak, but no cycle. – Bergi Jun 04 '18 at 12:40
  • @Bergi- Ok i have 2 last questions. In the link i provided, in the closures section. No one is refering to unused, so why is there a leak? And my second question: In section Forgotten timers or callbacks (second example) why this is a cycle reference?. Thank you – Nick Jun 04 '18 at 12:53
  • @Nick "*No one is referring to unused, so why is there a leak?*" - Because of a compiler bug. (In detail: because `unused` and `someMethod` (potentially) close over the same variables, the compiler creates a variable frame that contains all variables referenced in any closure regardless of the closure's usage, leading to `someMethod` keeping the `originalThing` alive). "*In second example of "Forgotten timers or callbacks" why this is a cycle reference?*" - because the `element` references the `onclick` callback that was installed on it, and the callback closes over the `element` variable. – Bergi Jun 04 '18 at 13:02
  • @Nick No idea, I'd need to search using the hints given in the article you linked. Maybe they didn't even report the issue, it's just how V8 works - see the link in my answer about GC of closures. – Bergi Jun 04 '18 at 13:20