You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

"this & object prototypes": Chapter 2 Clarification??

Open dwellisch opened this issue 9 years ago • 3 comments

I don't know if this makes sense or not. As I was trying to digest this chapter, I got sent down a rabbit hole when the call stack was mentioned in finding the call-site. I modified the example in the chapter to read like this.: function baz() { var a = 1; // call-stack is: baz // so, our call-site is in the global scope

 console.log( "baz" );

 console.log(this.a);
 bar(); // <-- call-site for `bar`

}

function bar() { var a = 2; // call-stack is: baz -> bar // so, our call-site is in baz

console.log( "bar" ); console.log(this.a); foo(); // <-- call-site for foo }

function foo() { var a = 3; // call-stack is: baz -> bar -> foo // so, our call-site is in bar

console.log( "foo" );
console.log(this.a);

} var a = 0; baz(); // <-- call-site for baz console.log(this.a);

I confused this.a with the local var a in each function, thinking that I should see baz, 1, bar, 2, foo 3, 0 which would be the output if I my console.log statements were console.log(a) (printing out the local variables in lexical scope for each function call).

Perhaps, it was just my brain confusing this, but if you think otherwise such that others could likewise get confused here, then my suggestion would be to point this out with a similar example.

dwellisch avatar May 05 '15 14:05 dwellisch

Use

<your code here>

to format your code, it will be easier to read. Thanks.

Nikitaw99 avatar May 10 '16 01:05 Nikitaw99

result (Chrome 50.0.2661.94 (64-bit), CentOS7):

baz
0
bar
0
foo
0
0

Which browser did you use?

lopatnov avatar May 11 '16 20:05 lopatnov

I was also confused by this and would prefer the comments suggested

IanFLee avatar Nov 18 '17 02:11 IanFLee