Method containing “this” keyword works different when it's called in another method vs when it's assigned...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How does the “this” keyword work?
23 answers
Having the following code, the output is undefined when calling the Play method from my object instance while I was expecting the 'this' keyword to reference the global variable and display "Football" in lack of a PlayGound's scope variable.
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
Now, invoking the Play() at constructor level, without passing it's reference to a local variable the output is "Football".
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
Can someone explain the reason why those two aproaches behave differently?
javascript constructor this ecmascript-5
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How does the “this” keyword work?
23 answers
Having the following code, the output is undefined when calling the Play method from my object instance while I was expecting the 'this' keyword to reference the global variable and display "Football" in lack of a PlayGound's scope variable.
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
Now, invoking the Play() at constructor level, without passing it's reference to a local variable the output is "Football".
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
Can someone explain the reason why those two aproaches behave differently?
javascript constructor this ecmascript-5
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
It doesn't matter where it is called, it matters how it is called -obj.Play()
vs.Play()
. You also should use strict mode.
– Bergi
Nov 24 '18 at 17:53
add a comment |
This question already has an answer here:
How does the “this” keyword work?
23 answers
Having the following code, the output is undefined when calling the Play method from my object instance while I was expecting the 'this' keyword to reference the global variable and display "Football" in lack of a PlayGound's scope variable.
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
Now, invoking the Play() at constructor level, without passing it's reference to a local variable the output is "Football".
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
Can someone explain the reason why those two aproaches behave differently?
javascript constructor this ecmascript-5
This question already has an answer here:
How does the “this” keyword work?
23 answers
Having the following code, the output is undefined when calling the Play method from my object instance while I was expecting the 'this' keyword to reference the global variable and display "Football" in lack of a PlayGound's scope variable.
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
Now, invoking the Play() at constructor level, without passing it's reference to a local variable the output is "Football".
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
Can someone explain the reason why those two aproaches behave differently?
This question already has an answer here:
How does the “this” keyword work?
23 answers
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
javascript constructor this ecmascript-5
javascript constructor this ecmascript-5
edited Nov 24 '18 at 17:20
Patrick Roberts
21.3k33777
21.3k33777
asked Nov 24 '18 at 17:19
george sacageorge saca
205
205
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
It doesn't matter where it is called, it matters how it is called -obj.Play()
vs.Play()
. You also should use strict mode.
– Bergi
Nov 24 '18 at 17:53
add a comment |
1
It doesn't matter where it is called, it matters how it is called -obj.Play()
vs.Play()
. You also should use strict mode.
– Bergi
Nov 24 '18 at 17:53
1
1
It doesn't matter where it is called, it matters how it is called -
obj.Play()
vs. Play()
. You also should use strict mode.– Bergi
Nov 24 '18 at 17:53
It doesn't matter where it is called, it matters how it is called -
obj.Play()
vs. Play()
. You also should use strict mode.– Bergi
Nov 24 '18 at 17:53
add a comment |
1 Answer
1
active
oldest
votes
The first one outputs undefined
very simply because obj
does not have a game
property. That can be fixed like this:
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.
If you change the second snippet to strict mode it will error because this
in simple calls is undefined
instead of window
:
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
If you use let
or const
declaration for game
, the second snippet will output undefined
because those top-level declarations do not attach to the global scope window
:
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first one outputs undefined
very simply because obj
does not have a game
property. That can be fixed like this:
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.
If you change the second snippet to strict mode it will error because this
in simple calls is undefined
instead of window
:
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
If you use let
or const
declaration for game
, the second snippet will output undefined
because those top-level declarations do not attach to the global scope window
:
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
add a comment |
The first one outputs undefined
very simply because obj
does not have a game
property. That can be fixed like this:
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.
If you change the second snippet to strict mode it will error because this
in simple calls is undefined
instead of window
:
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
If you use let
or const
declaration for game
, the second snippet will output undefined
because those top-level declarations do not attach to the global scope window
:
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
add a comment |
The first one outputs undefined
very simply because obj
does not have a game
property. That can be fixed like this:
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.
If you change the second snippet to strict mode it will error because this
in simple calls is undefined
instead of window
:
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
If you use let
or const
declaration for game
, the second snippet will output undefined
because those top-level declarations do not attach to the global scope window
:
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
The first one outputs undefined
very simply because obj
does not have a game
property. That can be fixed like this:
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.
If you change the second snippet to strict mode it will error because this
in simple calls is undefined
instead of window
:
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
If you use let
or const
declaration for game
, the second snippet will output undefined
because those top-level declarations do not attach to the global scope window
:
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.game = game;
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
"use strict";
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
let game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
answered Nov 24 '18 at 17:31
Patrick RobertsPatrick Roberts
21.3k33777
21.3k33777
add a comment |
add a comment |
1
It doesn't matter where it is called, it matters how it is called -
obj.Play()
vs.Play()
. You also should use strict mode.– Bergi
Nov 24 '18 at 17:53