Javascript passing this to functionsJQuery $(this) selector function and limitationsHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Building a road to escape Earth's gravity by making a pyramid on Antartica
2.8 is missing the Carve option in the Boolean Modifier
Are there any existing monsters I can use as a basis for a baby skeleton statblock?
How to make a setting relevant?
Can characters escape from Death House through this method?
Traffic law UK, pedestrians
Do the English have an ancient (obsolete) verb for the action of the book opening?
Why don't B747s start takeoffs with full throttle?
Approximate solutions to non polynomial equations
Can an Eldritch Knight use Action Surge and thus Arcane Charge even when surprised?
Does the first version of Linux developed by Linus Torvalds have a GUI?
What's the correct term for a waitress in the Middle Ages?
Why does this sentence use 东西?
What are the words for people who cause trouble believing they know better?
SF novella separating the dumb majority from the intelligent part of mankind
Can you really not move between grapples/shoves?
What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?
The economics of a "no deal" Brexit
What can plausibly explain many of my very long and low-tech bridges?
Implement Homestuck's Catenative Doomsday Dice Cascader
Bent spoke design wheels — feasible?
How bad would a partial hash leak be, realistically?
Is it recommended against to open-source the code of a webapp?
How to generate random points without duplication?
Javascript passing this to functions
JQuery $(this) selector function and limitationsHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm not used to working with this
and trying to make some simple functions pass it back and forth. I'm not quite sure what javascript is expecting, but I don't think I'm doing it right.
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs(this)
// this does not
)
function pastJobs()
var x = $(this).find('.past-positions ol').children()
console.log(x)
// this prints as undefined
I assume its possible to pass this
to functions, but I don't think I'm doing it in the right way.
What am I doing wrong?
javascript variables this
add a comment |
I'm not used to working with this
and trying to make some simple functions pass it back and forth. I'm not quite sure what javascript is expecting, but I don't think I'm doing it right.
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs(this)
// this does not
)
function pastJobs()
var x = $(this).find('.past-positions ol').children()
console.log(x)
// this prints as undefined
I assume its possible to pass this
to functions, but I don't think I'm doing it in the right way.
What am I doing wrong?
javascript variables this
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
this
and$(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called
– Davide Vitali
Mar 24 at 15:31
add a comment |
I'm not used to working with this
and trying to make some simple functions pass it back and forth. I'm not quite sure what javascript is expecting, but I don't think I'm doing it right.
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs(this)
// this does not
)
function pastJobs()
var x = $(this).find('.past-positions ol').children()
console.log(x)
// this prints as undefined
I assume its possible to pass this
to functions, but I don't think I'm doing it in the right way.
What am I doing wrong?
javascript variables this
I'm not used to working with this
and trying to make some simple functions pass it back and forth. I'm not quite sure what javascript is expecting, but I don't think I'm doing it right.
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs(this)
// this does not
)
function pastJobs()
var x = $(this).find('.past-positions ol').children()
console.log(x)
// this prints as undefined
I assume its possible to pass this
to functions, but I don't think I'm doing it in the right way.
What am I doing wrong?
javascript variables this
javascript variables this
edited Mar 24 at 17:11
Rizwan
13412
13412
asked Mar 24 at 15:18
Morgan AllenMorgan Allen
1,15731945
1,15731945
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
this
and$(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called
– Davide Vitali
Mar 24 at 15:31
add a comment |
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
this
and$(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called
– Davide Vitali
Mar 24 at 15:31
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
this
and $(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called– Davide Vitali
Mar 24 at 15:31
this
and $(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called– Davide Vitali
Mar 24 at 15:31
add a comment |
3 Answers
3
active
oldest
votes
Try pastJobs.call(this)
instead.
add a comment |
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
Is there a reason to usebind()
followed by()
instead ofcall()
?
– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the functionbind
creates a new function. Probably, the best approach are the functionscall
andapply
.
– Ele
Mar 24 at 15:42
add a comment |
pastJobs(this)
you are passing this
as an argument
and you're function doesn't accept arguments function pastJobs()
. so doing $(this)
in pastJobs
is really out of context.
you could call the function .call(this)
/.apply(this)
, or bind()
and then call it. (bind only binds this object but unlike apply or call doens't invoke the function.
keep in mind that call
and apply
takes arguments after this
object in a different manner. The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
you need something like
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs.call(this);
// this does not
)
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%2f55325271%2fjavascript-passing-this-to-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try pastJobs.call(this)
instead.
add a comment |
Try pastJobs.call(this)
instead.
add a comment |
Try pastJobs.call(this)
instead.
Try pastJobs.call(this)
instead.
answered Mar 24 at 15:20
SombriksSombriks
1,58432332
1,58432332
add a comment |
add a comment |
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
Is there a reason to usebind()
followed by()
instead ofcall()
?
– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the functionbind
creates a new function. Probably, the best approach are the functionscall
andapply
.
– Ele
Mar 24 at 15:42
add a comment |
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
Is there a reason to usebind()
followed by()
instead ofcall()
?
– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the functionbind
creates a new function. Probably, the best approach are the functionscall
andapply
.
– Ele
Mar 24 at 15:42
add a comment |
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
answered Mar 24 at 15:20
EleEle
26.3k52354
26.3k52354
Is there a reason to usebind()
followed by()
instead ofcall()
?
– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the functionbind
creates a new function. Probably, the best approach are the functionscall
andapply
.
– Ele
Mar 24 at 15:42
add a comment |
Is there a reason to usebind()
followed by()
instead ofcall()
?
– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the functionbind
creates a new function. Probably, the best approach are the functionscall
andapply
.
– Ele
Mar 24 at 15:42
Is there a reason to use
bind()
followed by ()
instead of call()
?– Mark Meyer
Mar 24 at 15:34
Is there a reason to use
bind()
followed by ()
instead of call()
?– Mark Meyer
Mar 24 at 15:34
@MarkMeyer no, as you may know, the function
bind
creates a new function. Probably, the best approach are the functions call
and apply
.– Ele
Mar 24 at 15:42
@MarkMeyer no, as you may know, the function
bind
creates a new function. Probably, the best approach are the functions call
and apply
.– Ele
Mar 24 at 15:42
add a comment |
pastJobs(this)
you are passing this
as an argument
and you're function doesn't accept arguments function pastJobs()
. so doing $(this)
in pastJobs
is really out of context.
you could call the function .call(this)
/.apply(this)
, or bind()
and then call it. (bind only binds this object but unlike apply or call doens't invoke the function.
keep in mind that call
and apply
takes arguments after this
object in a different manner. The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
you need something like
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs.call(this);
// this does not
)
add a comment |
pastJobs(this)
you are passing this
as an argument
and you're function doesn't accept arguments function pastJobs()
. so doing $(this)
in pastJobs
is really out of context.
you could call the function .call(this)
/.apply(this)
, or bind()
and then call it. (bind only binds this object but unlike apply or call doens't invoke the function.
keep in mind that call
and apply
takes arguments after this
object in a different manner. The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
you need something like
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs.call(this);
// this does not
)
add a comment |
pastJobs(this)
you are passing this
as an argument
and you're function doesn't accept arguments function pastJobs()
. so doing $(this)
in pastJobs
is really out of context.
you could call the function .call(this)
/.apply(this)
, or bind()
and then call it. (bind only binds this object but unlike apply or call doens't invoke the function.
keep in mind that call
and apply
takes arguments after this
object in a different manner. The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
you need something like
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs.call(this);
// this does not
)
pastJobs(this)
you are passing this
as an argument
and you're function doesn't accept arguments function pastJobs()
. so doing $(this)
in pastJobs
is really out of context.
you could call the function .call(this)
/.apply(this)
, or bind()
and then call it. (bind only binds this object but unlike apply or call doens't invoke the function.
keep in mind that call
and apply
takes arguments after this
object in a different manner. The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
you need something like
$(".search-result").each(function()
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs.call(this);
// this does not
)
edited Mar 24 at 15:32
answered Mar 24 at 15:26
FedeScFedeSc
1,1191128
1,1191128
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%2f55325271%2fjavascript-passing-this-to-functions%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
stackoverflow.com/questions/5611233/…
– Paul McLoughlin
Mar 24 at 15:20
this
and$(this)
are two very different things, as the latter is a specific jQuery object while the first change on the how the object is called– Davide Vitali
Mar 24 at 15:31