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;








2















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?










share|improve this question
























  • 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

















2















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?










share|improve this question
























  • 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













2












2








2








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












3 Answers
3






active

oldest

votes


















4














Try pastJobs.call(this) instead.






share|improve this answer






























    1














    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)()





    share|improve this answer























    • 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


















    1














    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
    )





    share|improve this answer

























      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
      );



      );













      draft saved

      draft discarded


















      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









      4














      Try pastJobs.call(this) instead.






      share|improve this answer



























        4














        Try pastJobs.call(this) instead.






        share|improve this answer

























          4












          4








          4







          Try pastJobs.call(this) instead.






          share|improve this answer













          Try pastJobs.call(this) instead.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 15:20









          SombriksSombriks

          1,58432332




          1,58432332























              1














              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)()





              share|improve this answer























              • 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















              1














              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)()





              share|improve this answer























              • 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













              1












              1








              1







              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)()





              share|improve this answer













              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)()






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 24 at 15:20









              EleEle

              26.3k52354




              26.3k52354












              • 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

















              • 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
















              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











              1














              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
              )





              share|improve this answer





























                1














                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
                )





                share|improve this answer



























                  1












                  1








                  1







                  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
                  )





                  share|improve this answer















                  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
                  )






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 24 at 15:32

























                  answered Mar 24 at 15:26









                  FedeScFedeSc

                  1,1191128




                  1,1191128



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현