Understanding the JS code while setting up redis with Mongoose





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I was trying to understand caching in NodeJS using redis.



In the lecture, the instructor told that best place to setup caching would be just before exec function in mongoose.



So in order to do that he did



const mongoose = require('mongoose')

const exec = mongoose.Query.prototype.exec;
//Redis logic
mongoose.Query.prototype.exec = function () {
console.log("i am here")
return exec.apply(this, argument);
}


1st: What will classify mongoose.Query.prototype.exec; as? value type or reference type? Because if it is a reference type then when we change mongoose.Query.prototype.exec = function then shouldn't its value change as well?



2nd I am unable to comprehend this line here return exec.apply(this, argument); Can someone explain this in stretch i.e this in apply points to where? and he is passing argument (this, argument); where does that Argument come from?



Can someone please help me out by answering both the above question?










share|improve this question































    0















    I was trying to understand caching in NodeJS using redis.



    In the lecture, the instructor told that best place to setup caching would be just before exec function in mongoose.



    So in order to do that he did



    const mongoose = require('mongoose')

    const exec = mongoose.Query.prototype.exec;
    //Redis logic
    mongoose.Query.prototype.exec = function () {
    console.log("i am here")
    return exec.apply(this, argument);
    }


    1st: What will classify mongoose.Query.prototype.exec; as? value type or reference type? Because if it is a reference type then when we change mongoose.Query.prototype.exec = function then shouldn't its value change as well?



    2nd I am unable to comprehend this line here return exec.apply(this, argument); Can someone explain this in stretch i.e this in apply points to where? and he is passing argument (this, argument); where does that Argument come from?



    Can someone please help me out by answering both the above question?










    share|improve this question



























      0












      0








      0








      I was trying to understand caching in NodeJS using redis.



      In the lecture, the instructor told that best place to setup caching would be just before exec function in mongoose.



      So in order to do that he did



      const mongoose = require('mongoose')

      const exec = mongoose.Query.prototype.exec;
      //Redis logic
      mongoose.Query.prototype.exec = function () {
      console.log("i am here")
      return exec.apply(this, argument);
      }


      1st: What will classify mongoose.Query.prototype.exec; as? value type or reference type? Because if it is a reference type then when we change mongoose.Query.prototype.exec = function then shouldn't its value change as well?



      2nd I am unable to comprehend this line here return exec.apply(this, argument); Can someone explain this in stretch i.e this in apply points to where? and he is passing argument (this, argument); where does that Argument come from?



      Can someone please help me out by answering both the above question?










      share|improve this question
















      I was trying to understand caching in NodeJS using redis.



      In the lecture, the instructor told that best place to setup caching would be just before exec function in mongoose.



      So in order to do that he did



      const mongoose = require('mongoose')

      const exec = mongoose.Query.prototype.exec;
      //Redis logic
      mongoose.Query.prototype.exec = function () {
      console.log("i am here")
      return exec.apply(this, argument);
      }


      1st: What will classify mongoose.Query.prototype.exec; as? value type or reference type? Because if it is a reference type then when we change mongoose.Query.prototype.exec = function then shouldn't its value change as well?



      2nd I am unable to comprehend this line here return exec.apply(this, argument); Can someone explain this in stretch i.e this in apply points to where? and he is passing argument (this, argument); where does that Argument come from?



      Can someone please help me out by answering both the above question?







      javascript node.js mongoose redis






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 0:32









      Neil Lunn

      101k23181188




      101k23181188










      asked Nov 22 '18 at 23:14









      iRohitBhatiaiRohitBhatia

      1,4521736




      1,4521736
























          1 Answer
          1






          active

          oldest

          votes


















          3















          What will classify mongoose.Query.prototype.exec; as? value type or
          reference type? Because if it is a reference type then when we change
          mongoose.Query.prototype.exec = function




          exec is of reference type, but it is assigned the value of another reference variable mongoose.Query.prototype.exec. You can think of it like this : mongoose.Query.prototype.exec is itself pointing to an object (a Function object) in memory, and now after the assignment, exec is also pointing to the same object - in other words, the memory address of the object is copied (by value) from mongoose.Query.prototype.exec to exec during assignment. So the value of the variable mongoose.Query.prototype.exec itself i.e. the memory address stored in it, can be changed without affecting the other variable exec. They both will just end up pointing to two different objects.




          Can someone explain this in stretch i.e this in apply points to where?




          In this case, it'll be the object on which this function will be invoked i.e. the Query instance.




          and he is passing argument (this, argument); where does that Argument come from?




          Unless there is some code you missed to copy paste in the question, argument appears to be a typo. He was probably referring to the built-in object arguments which is accessible inside every function and consists of the arguments passed to the function. Here is a reference.



          At a high level, what the instructor is trying to do is to override the built-in behavior of the function Query.exec() to add some of his own custom processing. He first creates a "backup" of the original function, then points Query.exec to his custom function which adds the custom processing (the log statement) and then hands over control to the backup i.e. proceed with built-in behavior. Whoever invokes exec() on a Query instance after this point will see the overridden functionality - first a log statement, then built-in behavior of exec()






          share|improve this answer
























          • amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

            – iRohitBhatia
            Nov 23 '18 at 14:17











          • @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

            – Vasan
            Nov 23 '18 at 17:35












          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%2f53438970%2funderstanding-the-js-code-while-setting-up-redis-with-mongoose%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3















          What will classify mongoose.Query.prototype.exec; as? value type or
          reference type? Because if it is a reference type then when we change
          mongoose.Query.prototype.exec = function




          exec is of reference type, but it is assigned the value of another reference variable mongoose.Query.prototype.exec. You can think of it like this : mongoose.Query.prototype.exec is itself pointing to an object (a Function object) in memory, and now after the assignment, exec is also pointing to the same object - in other words, the memory address of the object is copied (by value) from mongoose.Query.prototype.exec to exec during assignment. So the value of the variable mongoose.Query.prototype.exec itself i.e. the memory address stored in it, can be changed without affecting the other variable exec. They both will just end up pointing to two different objects.




          Can someone explain this in stretch i.e this in apply points to where?




          In this case, it'll be the object on which this function will be invoked i.e. the Query instance.




          and he is passing argument (this, argument); where does that Argument come from?




          Unless there is some code you missed to copy paste in the question, argument appears to be a typo. He was probably referring to the built-in object arguments which is accessible inside every function and consists of the arguments passed to the function. Here is a reference.



          At a high level, what the instructor is trying to do is to override the built-in behavior of the function Query.exec() to add some of his own custom processing. He first creates a "backup" of the original function, then points Query.exec to his custom function which adds the custom processing (the log statement) and then hands over control to the backup i.e. proceed with built-in behavior. Whoever invokes exec() on a Query instance after this point will see the overridden functionality - first a log statement, then built-in behavior of exec()






          share|improve this answer
























          • amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

            – iRohitBhatia
            Nov 23 '18 at 14:17











          • @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

            – Vasan
            Nov 23 '18 at 17:35
















          3















          What will classify mongoose.Query.prototype.exec; as? value type or
          reference type? Because if it is a reference type then when we change
          mongoose.Query.prototype.exec = function




          exec is of reference type, but it is assigned the value of another reference variable mongoose.Query.prototype.exec. You can think of it like this : mongoose.Query.prototype.exec is itself pointing to an object (a Function object) in memory, and now after the assignment, exec is also pointing to the same object - in other words, the memory address of the object is copied (by value) from mongoose.Query.prototype.exec to exec during assignment. So the value of the variable mongoose.Query.prototype.exec itself i.e. the memory address stored in it, can be changed without affecting the other variable exec. They both will just end up pointing to two different objects.




          Can someone explain this in stretch i.e this in apply points to where?




          In this case, it'll be the object on which this function will be invoked i.e. the Query instance.




          and he is passing argument (this, argument); where does that Argument come from?




          Unless there is some code you missed to copy paste in the question, argument appears to be a typo. He was probably referring to the built-in object arguments which is accessible inside every function and consists of the arguments passed to the function. Here is a reference.



          At a high level, what the instructor is trying to do is to override the built-in behavior of the function Query.exec() to add some of his own custom processing. He first creates a "backup" of the original function, then points Query.exec to his custom function which adds the custom processing (the log statement) and then hands over control to the backup i.e. proceed with built-in behavior. Whoever invokes exec() on a Query instance after this point will see the overridden functionality - first a log statement, then built-in behavior of exec()






          share|improve this answer
























          • amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

            – iRohitBhatia
            Nov 23 '18 at 14:17











          • @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

            – Vasan
            Nov 23 '18 at 17:35














          3












          3








          3








          What will classify mongoose.Query.prototype.exec; as? value type or
          reference type? Because if it is a reference type then when we change
          mongoose.Query.prototype.exec = function




          exec is of reference type, but it is assigned the value of another reference variable mongoose.Query.prototype.exec. You can think of it like this : mongoose.Query.prototype.exec is itself pointing to an object (a Function object) in memory, and now after the assignment, exec is also pointing to the same object - in other words, the memory address of the object is copied (by value) from mongoose.Query.prototype.exec to exec during assignment. So the value of the variable mongoose.Query.prototype.exec itself i.e. the memory address stored in it, can be changed without affecting the other variable exec. They both will just end up pointing to two different objects.




          Can someone explain this in stretch i.e this in apply points to where?




          In this case, it'll be the object on which this function will be invoked i.e. the Query instance.




          and he is passing argument (this, argument); where does that Argument come from?




          Unless there is some code you missed to copy paste in the question, argument appears to be a typo. He was probably referring to the built-in object arguments which is accessible inside every function and consists of the arguments passed to the function. Here is a reference.



          At a high level, what the instructor is trying to do is to override the built-in behavior of the function Query.exec() to add some of his own custom processing. He first creates a "backup" of the original function, then points Query.exec to his custom function which adds the custom processing (the log statement) and then hands over control to the backup i.e. proceed with built-in behavior. Whoever invokes exec() on a Query instance after this point will see the overridden functionality - first a log statement, then built-in behavior of exec()






          share|improve this answer














          What will classify mongoose.Query.prototype.exec; as? value type or
          reference type? Because if it is a reference type then when we change
          mongoose.Query.prototype.exec = function




          exec is of reference type, but it is assigned the value of another reference variable mongoose.Query.prototype.exec. You can think of it like this : mongoose.Query.prototype.exec is itself pointing to an object (a Function object) in memory, and now after the assignment, exec is also pointing to the same object - in other words, the memory address of the object is copied (by value) from mongoose.Query.prototype.exec to exec during assignment. So the value of the variable mongoose.Query.prototype.exec itself i.e. the memory address stored in it, can be changed without affecting the other variable exec. They both will just end up pointing to two different objects.




          Can someone explain this in stretch i.e this in apply points to where?




          In this case, it'll be the object on which this function will be invoked i.e. the Query instance.




          and he is passing argument (this, argument); where does that Argument come from?




          Unless there is some code you missed to copy paste in the question, argument appears to be a typo. He was probably referring to the built-in object arguments which is accessible inside every function and consists of the arguments passed to the function. Here is a reference.



          At a high level, what the instructor is trying to do is to override the built-in behavior of the function Query.exec() to add some of his own custom processing. He first creates a "backup" of the original function, then points Query.exec to his custom function which adds the custom processing (the log statement) and then hands over control to the backup i.e. proceed with built-in behavior. Whoever invokes exec() on a Query instance after this point will see the overridden functionality - first a log statement, then built-in behavior of exec()







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 0:00









          VasanVasan

          3,77131433




          3,77131433













          • amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

            – iRohitBhatia
            Nov 23 '18 at 14:17











          • @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

            – Vasan
            Nov 23 '18 at 17:35



















          • amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

            – iRohitBhatia
            Nov 23 '18 at 14:17











          • @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

            – Vasan
            Nov 23 '18 at 17:35

















          amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

          – iRohitBhatia
          Nov 23 '18 at 14:17





          amazing answer. I will continue watching probably he will talk about argument as we move on or I will run the code without passing arguments to see if it works as intended. Can you please elaborate more on apply part?

          – iRohitBhatia
          Nov 23 '18 at 14:17













          @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

          – Vasan
          Nov 23 '18 at 17:35





          @KuchBhi Not sure if I can explain it any better than the great MDN. Please let me know if you have questions in that doc.

          – Vasan
          Nov 23 '18 at 17:35




















          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%2f53438970%2funderstanding-the-js-code-while-setting-up-redis-with-mongoose%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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents