JavaScript Javascript: cached function property vs new value javascript cache function result,javascript cache function,clear cache function

A basic function:

function myStuff(a,b,c){
    var _c = _c || c || {};
    console.log(_c);
}

The idea is to use a cached value if used before, or a new value if new/different.

The question is: if the function invoke does not include the third property why the console.log(_c) shows undefined or how can I write this line var _c = _c || c || {}; better?

Thanks

Answer:1

the scope of the variable you defined is the function itself, therefore, it is redefined every time you call the function.

the correct way to cache a variable, is to define it in the parent scope of the function:

var _c;
function myStuff(a,b,c){
    _c = c || _c || {};
    console.log(_c);
}

read more about js scopes here: What is the scope of variables in JavaScript?

Answer:2

You can store the cache inside the function itself

function myStuff(a,b,c){
    if (!myStuff.cache) {
        myStuff.cache = {};
    }

    var cache = myStuff.cache;
    c = cache.c = cache.c || c || {};

    console.log(c);
}
Answer:3

If yout want to access _c external value you have to use the function like this:

function myStuff(a,b,c){
    var _c = window._c || c || {};
    console.log(_c);
}
Answer:4

Can phantomjs perform actions like getText() or enterText() inside iFrame? I am using protractor to do my testing. Phantomjs can perform actions in the default frame but inside iframe the locators get ...

Can phantomjs perform actions like getText() or enterText() inside iFrame? I am using protractor to do my testing. Phantomjs can perform actions in the default frame but inside iframe the locators get ...

  1. accessing elements inside iframe

I have this JQuery script that is looking at some DIVs and getting the image sizes and setting the div size according to the images. I had a JQuery script to get all the DIVs and loop to each image ...

I have this JQuery script that is looking at some DIVs and getting the image sizes and setting the div size according to the images. I had a JQuery script to get all the DIVs and loop to each image ...

  1. optimize javascript jquery

I am using: Grunt with the watch plugin for live reload & Sublime Text 2's SFTP plugin for uploading files on save Live reload is just a little quicker than the SFTP upload. Using Guard, you ...

I am using: Grunt with the watch plugin for live reload & Sublime Text 2's SFTP plugin for uploading files on save Live reload is just a little quicker than the SFTP upload. Using Guard, you ...

  1. grunt livereload not working
  2. grunt livereload example
  3. grunt livereload port
  4. grunt livereload
  5. grunt livereload change port
  6. grunt livereload browser
  7. grunt livereload options
  8. grunt livereload chrome
  9. grunt livereload watch
  10. grunt livereload not reloading
  11. grunt livereload middleware
  12. grunt watch live reload
  13. magento 2 grunt livereload
  14. grunt connect livereload
  15. grunt connect livereload not working
  16. grunt watch livereload example
  17. grunt-contrib-connect livereload
  18. grunt server livereload
  19. grunt watch livereload not working
  20. grunt connect livereload example

I have a bootstrap modal contact form which uses AJAX and PHP to save the information sent by a user to a database: <div class="modal fade" id="contact" role="dialogue"> <div ...

I have a bootstrap modal contact form which uses AJAX and PHP to save the information sent by a user to a database: <div class="modal fade" id="contact" role="dialogue"> <div ...