I understand the later, we call the alert on the foo
object, which has another object named baz
as its property, which in turn has a method named bar
that returns the value of x
. And because of lexical scope
(I think :) ) the JS compiler/interpreter goes up the chain, finds x
in baz
and returns 1.
My guess is when assigned to the variable go
and then called from the global scope, you get 3? Just want to find out what's happening in the background. Any help will be appreciated!!!
var x = 3;
var foo = {
x: 2,
baz: {
x: 1,
bar: function() {
return this.x;
}
}
}
var go = foo.baz.bar;
alert(go());
alert(foo.baz.bar());
A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.
The return statement is used to return a particular value from the function to the function caller. The function will stop executing when the return statement is called. The return statement should be the last statement in a function because the code after the return statement will be unreachable.
The value that is returned from a call to a function is the value of that function call. For example, with that function, you can set a variable to the value that is returned using the following code (which would set result to 5). var result = num(3,3);
When you do something like this:
var go = foo.baz.bar;
go();
you will find that go
has lost the reference to foo.baz
before calling bar()
. It is just a pointer to the bar
function and has no association with the object that it is attached to any more. That means that this
will not be foo.baz
when the bar
method executes.
This is explicitly what .bind()
was developed for. You can use it like this:
var go = foo.baz.bar.bind(foo.baz);
go();
And, it will then work for you. You can also do the manual version of the same thing:
var go = function() {return foo.baz.bar();}
go();
but .bind()
is built into the language now to help you solve this type of issue.
In first you declare a function expression to a variable with name go
. If you execute function go
this
refers to the global object and there the variable x
has value 3 so that's why it alert(go())
alerts 3. On the other hand you execute the method foo.baz.bar()
. Here this
refer to the object(foo
) and the x
has the value 1
. So it alerts 1
.
var x = 3;
var foo = {
x: 2,
baz: {
x: 1,
bar: function() {
return this.x;
}
}
}
//here you save a function expression
//to a variablewith name go
var go = foo.baz.bar;
//you execute go but this refer to the global object
//and x has the value of 3 in the global object
console.log(go());//this will output 3
//this refer to the object foo where x has
//the value of 1
console.log(foo.baz.bar());//this will output 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With