call() a function within its own contextIs there an “exists” function for jQuery?var functionName = function() vs function functionName() Is JavaScript a pass-by-reference or pass-by-value language?JavaScript closure inside loops – simple practical exampleSet a default parameter value for a JavaScript functionWhat is the purpose of the var keyword and when should I use it (or omit it)?How to break/exit from a each() function in JQuery?What is the difference between call and apply?How do I return the response from an asynchronous call?Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
Is every story set in the future "science fiction"?
Is there a need for better software for writers?
Watching the game, having a puzzle
Is a vertical stabiliser needed for straight line flight in a glider?
Why are parallelograms defined as quadrilaterals? What term would encompass polygons with greater than two parallel pairs?
Extending Kan fibrations, without using minimal fibrations
Thesis' "Future Work" section – is it acceptable to omit personal involvement in a mentioned project?
Is there any evidence to support the claim that the United States was "suckered into WW1" by Zionists, made by Benjamin Freedman in his 1961 speech
What do "KAL." and "A.S." stand for in this inscription?
How to handle DM constantly stealing everything from sleeping characters?
No such column 'DeveloperName' on entity 'RecordType' after Summer '19 release on sandbox
Remove color cast in darktable?
Would encrypting a database protect against a compromised admin account?
How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?
Names of the Six Tastes
How to efficiently lower your karma
How to select certain lines (n, n+4, n+8, n+12...) from the file?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
Has there been evidence of any other gods?
Best species to breed to intelligence
What can cause an unfrozen indoor copper drain pipe to crack?
Passport stamps art, can it be done?
Improving Sati-Sampajañña (situative wisdom)
Why was wildfire not used during the Battle of Winterfell?
call() a function within its own context
Is there an “exists” function for jQuery?var functionName = function() vs function functionName() Is JavaScript a pass-by-reference or pass-by-value language?JavaScript closure inside loops – simple practical exampleSet a default parameter value for a JavaScript functionWhat is the purpose of the var keyword and when should I use it (or omit it)?How to break/exit from a each() function in JQuery?What is the difference between call and apply?How do I return the response from an asynchronous call?Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
Running var f
as f.call(f)
outputs 5
. When running it as f()
or f.call()
outputs 3
.
What happens in each case? What does the inner function's this
refer to?
javascript this
add a comment |
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
Running var f
as f.call(f)
outputs 5
. When running it as f()
or f.call()
outputs 3
.
What happens in each case? What does the inner function's this
refer to?
javascript this
3
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
3
If you want to add some other scopes/context, use the.bind(context)
method.
– Adrian Preuss
May 6 at 15:28
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
1
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21
add a comment |
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
Running var f
as f.call(f)
outputs 5
. When running it as f()
or f.call()
outputs 3
.
What happens in each case? What does the inner function's this
refer to?
javascript this
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
Running var f
as f.call(f)
outputs 5
. When running it as f()
or f.call()
outputs 3
.
What happens in each case? What does the inner function's this
refer to?
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
var f = function()
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f);
f();
f.call();
javascript this
javascript this
edited May 6 at 15:14
Derek Pollard
4,45252741
4,45252741
asked May 6 at 15:11
El AnonimoEl Anonimo
355415
355415
3
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
3
If you want to add some other scopes/context, use the.bind(context)
method.
– Adrian Preuss
May 6 at 15:28
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
1
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21
add a comment |
3
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
3
If you want to add some other scopes/context, use the.bind(context)
method.
– Adrian Preuss
May 6 at 15:28
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
1
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21
3
3
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
3
3
If you want to add some other scopes/context, use the
.bind(context)
method.– Adrian Preuss
May 6 at 15:28
If you want to add some other scopes/context, use the
.bind(context)
method.– Adrian Preuss
May 6 at 15:28
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
1
1
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21
add a comment |
4 Answers
4
active
oldest
votes
First Case:
In the first you are calling the function. And inside the function the function itself i.e f
is set as this
. So in first example this.x = 5;
sets the property x
on the function.
When the inner function is called this
refers to window
object so this.x = 3;
changes the x
property of window object.
When it logs console.log(this.x);
here the same property x
which was set as property of function is logged.
Second Case:
In the second example this
inside the outer function refers to window
so when this.x = 3;
is evaluated the window.x
becomes 3
. As this
refers to window
in outer function so console.log(this.x);
logs window.x
which is 3
Conclusion:
The conclusion of the whole discussion is that if no argument is passed to call()
then automatically window
object is binded. According to MDN
thisArg
Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
See the below snippet.
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
add a comment |
If there is no specific context, this
will be window
. Your inner function always runs without a context, so it'll set window.x
to 3. If you call f()
, it will also run with this
being window
therefore logging the 3.
If you however do f.call(f)
, this
will be the f
function object, and it's x
property will be set to 5.
f.call(f)
console.log(
f.x, // 5
window.x // 3
);
I'd recommend stepping through it with the debugger if it isn't clear yet.
add a comment |
When you call f
with a reference to itself, it sets the x
property to 5 on the function and the inner anonymous function has its this
referring to the window, so it sets window.x to 3. Outside the anonymous function, this
still refers to the function f
, so console.log(this.x) outputs 5.
When you invoke f using f()
or f.call()
the function f and the anonymous function inside it have a this
reference set to the window
(the default) and so changing the value of this.x
inside or outside the anonymous function affects the output result.
You can see this clearly if you console.log the values of this
inside the function f and inside the inner anonymous function.
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
add a comment |
Further to the other answers, should you want predictable behaviour, you have at least 2 methods available to you.
Method 1: (closure)
var f = function()
this.x = 5;
var that = this;
(function()
that.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Method 2: (arrow function)
var f = () =>
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56007944%2fcall-a-function-within-its-own-context%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
First Case:
In the first you are calling the function. And inside the function the function itself i.e f
is set as this
. So in first example this.x = 5;
sets the property x
on the function.
When the inner function is called this
refers to window
object so this.x = 3;
changes the x
property of window object.
When it logs console.log(this.x);
here the same property x
which was set as property of function is logged.
Second Case:
In the second example this
inside the outer function refers to window
so when this.x = 3;
is evaluated the window.x
becomes 3
. As this
refers to window
in outer function so console.log(this.x);
logs window.x
which is 3
Conclusion:
The conclusion of the whole discussion is that if no argument is passed to call()
then automatically window
object is binded. According to MDN
thisArg
Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
See the below snippet.
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
add a comment |
First Case:
In the first you are calling the function. And inside the function the function itself i.e f
is set as this
. So in first example this.x = 5;
sets the property x
on the function.
When the inner function is called this
refers to window
object so this.x = 3;
changes the x
property of window object.
When it logs console.log(this.x);
here the same property x
which was set as property of function is logged.
Second Case:
In the second example this
inside the outer function refers to window
so when this.x = 3;
is evaluated the window.x
becomes 3
. As this
refers to window
in outer function so console.log(this.x);
logs window.x
which is 3
Conclusion:
The conclusion of the whole discussion is that if no argument is passed to call()
then automatically window
object is binded. According to MDN
thisArg
Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
See the below snippet.
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
add a comment |
First Case:
In the first you are calling the function. And inside the function the function itself i.e f
is set as this
. So in first example this.x = 5;
sets the property x
on the function.
When the inner function is called this
refers to window
object so this.x = 3;
changes the x
property of window object.
When it logs console.log(this.x);
here the same property x
which was set as property of function is logged.
Second Case:
In the second example this
inside the outer function refers to window
so when this.x = 3;
is evaluated the window.x
becomes 3
. As this
refers to window
in outer function so console.log(this.x);
logs window.x
which is 3
Conclusion:
The conclusion of the whole discussion is that if no argument is passed to call()
then automatically window
object is binded. According to MDN
thisArg
Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
See the below snippet.
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
First Case:
In the first you are calling the function. And inside the function the function itself i.e f
is set as this
. So in first example this.x = 5;
sets the property x
on the function.
When the inner function is called this
refers to window
object so this.x = 3;
changes the x
property of window object.
When it logs console.log(this.x);
here the same property x
which was set as property of function is logged.
Second Case:
In the second example this
inside the outer function refers to window
so when this.x = 3;
is evaluated the window.x
becomes 3
. As this
refers to window
in outer function so console.log(this.x);
logs window.x
which is 3
Conclusion:
The conclusion of the whole discussion is that if no argument is passed to call()
then automatically window
object is binded. According to MDN
thisArg
Optional. The value of this provided for the call to a function. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.
See the below snippet.
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
function foo()
console.log(this);
foo.call(foo); //foo function
foo.call(); //window object
edited May 6 at 15:37
answered May 6 at 15:21
Maheer AliMaheer Ali
15.7k11632
15.7k11632
add a comment |
add a comment |
If there is no specific context, this
will be window
. Your inner function always runs without a context, so it'll set window.x
to 3. If you call f()
, it will also run with this
being window
therefore logging the 3.
If you however do f.call(f)
, this
will be the f
function object, and it's x
property will be set to 5.
f.call(f)
console.log(
f.x, // 5
window.x // 3
);
I'd recommend stepping through it with the debugger if it isn't clear yet.
add a comment |
If there is no specific context, this
will be window
. Your inner function always runs without a context, so it'll set window.x
to 3. If you call f()
, it will also run with this
being window
therefore logging the 3.
If you however do f.call(f)
, this
will be the f
function object, and it's x
property will be set to 5.
f.call(f)
console.log(
f.x, // 5
window.x // 3
);
I'd recommend stepping through it with the debugger if it isn't clear yet.
add a comment |
If there is no specific context, this
will be window
. Your inner function always runs without a context, so it'll set window.x
to 3. If you call f()
, it will also run with this
being window
therefore logging the 3.
If you however do f.call(f)
, this
will be the f
function object, and it's x
property will be set to 5.
f.call(f)
console.log(
f.x, // 5
window.x // 3
);
I'd recommend stepping through it with the debugger if it isn't clear yet.
If there is no specific context, this
will be window
. Your inner function always runs without a context, so it'll set window.x
to 3. If you call f()
, it will also run with this
being window
therefore logging the 3.
If you however do f.call(f)
, this
will be the f
function object, and it's x
property will be set to 5.
f.call(f)
console.log(
f.x, // 5
window.x // 3
);
I'd recommend stepping through it with the debugger if it isn't clear yet.
edited May 6 at 15:31
answered May 6 at 15:23
Jonas WilmsJonas Wilms
69.6k63763
69.6k63763
add a comment |
add a comment |
When you call f
with a reference to itself, it sets the x
property to 5 on the function and the inner anonymous function has its this
referring to the window, so it sets window.x to 3. Outside the anonymous function, this
still refers to the function f
, so console.log(this.x) outputs 5.
When you invoke f using f()
or f.call()
the function f and the anonymous function inside it have a this
reference set to the window
(the default) and so changing the value of this.x
inside or outside the anonymous function affects the output result.
You can see this clearly if you console.log the values of this
inside the function f and inside the inner anonymous function.
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
add a comment |
When you call f
with a reference to itself, it sets the x
property to 5 on the function and the inner anonymous function has its this
referring to the window, so it sets window.x to 3. Outside the anonymous function, this
still refers to the function f
, so console.log(this.x) outputs 5.
When you invoke f using f()
or f.call()
the function f and the anonymous function inside it have a this
reference set to the window
(the default) and so changing the value of this.x
inside or outside the anonymous function affects the output result.
You can see this clearly if you console.log the values of this
inside the function f and inside the inner anonymous function.
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
add a comment |
When you call f
with a reference to itself, it sets the x
property to 5 on the function and the inner anonymous function has its this
referring to the window, so it sets window.x to 3. Outside the anonymous function, this
still refers to the function f
, so console.log(this.x) outputs 5.
When you invoke f using f()
or f.call()
the function f and the anonymous function inside it have a this
reference set to the window
(the default) and so changing the value of this.x
inside or outside the anonymous function affects the output result.
You can see this clearly if you console.log the values of this
inside the function f and inside the inner anonymous function.
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
When you call f
with a reference to itself, it sets the x
property to 5 on the function and the inner anonymous function has its this
referring to the window, so it sets window.x to 3. Outside the anonymous function, this
still refers to the function f
, so console.log(this.x) outputs 5.
When you invoke f using f()
or f.call()
the function f and the anonymous function inside it have a this
reference set to the window
(the default) and so changing the value of this.x
inside or outside the anonymous function affects the output result.
You can see this clearly if you console.log the values of this
inside the function f and inside the inner anonymous function.
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
var f = function()
console.log("This inside function f:", this.toString());
this.x = 5;
(function()
console.log("This inside anonymous inner function:", this.toString());
this.x = 3;
)();
console.log(this.x);
;
console.log("calling function x with this set to itself");
f.call(f);
console.log("---------------")
console.log("invoking function x with brackets ()")
f();
console.log("---------------")
console.log("calling function x without setting this context")
f.call();
console.log("---------------")
answered May 6 at 15:27
hev1hev1
6,0633729
6,0633729
add a comment |
add a comment |
Further to the other answers, should you want predictable behaviour, you have at least 2 methods available to you.
Method 1: (closure)
var f = function()
this.x = 5;
var that = this;
(function()
that.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Method 2: (arrow function)
var f = () =>
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
add a comment |
Further to the other answers, should you want predictable behaviour, you have at least 2 methods available to you.
Method 1: (closure)
var f = function()
this.x = 5;
var that = this;
(function()
that.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Method 2: (arrow function)
var f = () =>
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
add a comment |
Further to the other answers, should you want predictable behaviour, you have at least 2 methods available to you.
Method 1: (closure)
var f = function()
this.x = 5;
var that = this;
(function()
that.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Method 2: (arrow function)
var f = () =>
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Further to the other answers, should you want predictable behaviour, you have at least 2 methods available to you.
Method 1: (closure)
var f = function()
this.x = 5;
var that = this;
(function()
that.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
Method 2: (arrow function)
var f = () =>
this.x = 5;
(function()
this.x = 3;
)();
console.log(this.x);
;
f.call(f); // 3
f(); // 3
f.call(); // 3
edited 2 days ago
answered 2 days ago
Adrian BartholomewAdrian Bartholomew
70941125
70941125
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56007944%2fcall-a-function-within-its-own-context%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Don't confuse the two this references. Instead try to see there are two different stack frames. Each frame has a this reference. That this is either the default or it's been set by call(). The inner function can not be set. It's always the default. I think the default for this is window, but that depends if there is a module loader or not. I should know but I can't remember.
– cgTag
May 6 at 15:24
3
If you want to add some other scopes/context, use the
.bind(context)
method.– Adrian Preuss
May 6 at 15:28
This is why you always should use strict mode…
– Bergi
May 6 at 16:45
1
It's also why you should use lambdas instead of anonymous functions
– BlueRaja - Danny Pflughoeft
May 6 at 18:21