JavaScript Inner function cannot access outer function variable after being changed

Yes, this question is similar with Inner function cannot access outer functions variable. But it's not the same one. What confused me is "after being changed". Codes below maybe more intuitively.

var serial_maker = function() {
    var prefix = '',
        seq = 0;
    return {
        set_prefix: function(p) {
            prefix = String(p);
        },
        set_seq: function(s) {
            seq = s;
        },
        gensym: function() {
            var result = prefix + seq;
            seq += 1;
            return result;
        }
    };
};
var seqer = serial_maker();
seqer.set_prefix('Q');
seqer.set_seq(1000);
seqer.gensym(); // 'Q1000'

// Rewrite set_seq method
seqer.set_seq = function() {
    seq = 2000;
};
seqer.set_seq();

seqer.gensym(); // 'Q1001', which was expected 'Q2000'

So this is what i want to figure out —— why the rewritten set_seq method didn't change value of the private variable seq in outer function?

Hope i could get some essential answers, thanks =)

Answer:1

You are creating a new closure. The returned function seqer.set_seq is a closure which can access the private variable seq. But the newly defined seqer.set_seq is creating a new closure which cannot access the private variable, instead it creates a new global one window.seq

Try it:

var serial_maker = function() {
    var prefix = '',
        seq = 0;
    return {
        set_prefix: function(p) {
            prefix = String(p);
        },
        set_seq: function(s) {
            seq = s;
        },
        gensym: function() {
            var result = prefix + seq;
            seq += 1;
            return result;
        }
    };
};
var seqer = serial_maker();
seqer.set_prefix('Q');
seqer.set_seq(1000);
seqer.gensym(); // 'Q1000'

// Rewrite set_seq method
seqer.set_seq = function() {
    seq = 2000;
};
seqer.set_seq();

seqer.gensym(); // Q1001 is correct!

console.log(window.seq); // 2000
Answer:2

It's actually another seq variable. When you run set_seq for the first time, the seq variable is already bounded to it, it does not read it directly from the function declaration.

In other words, when you define your original function, an object with set_prefix, set_seq and gensym is returned, and each of these function already have a reference to the seq variable, which is defined in the function's closure. When you assign a new function to the returned object, it does not have the concept of the original seq variable.

Answer:3

I have a kendo grid in which one column can have null values. But I don't see the grid populating when there are null values. My code is here: $(document).ready(function() { $("#grid").kendoGrid({...

I have a kendo grid in which one column can have null values. But I don't see the grid populating when there are null values. My code is here: $(document).ready(function() { $("#grid").kendoGrid({...

  1. handling null value in json
  2. handling null value in c#
  3. handling null value in sql
  4. handling null value in java
  5. handling null value for datetime c#
  6. handling null value in python
  7. handling null value in vb.net
  8. handling null value in mysql
  9. handling null value in oracle
  10. handling null value in tableau
  11. handling the null value from a resultset in java
  12. java exception handling null value
  13. null value handling json c#
  14. null value handling
  15. null value handling in db2
  16. null value handling ignore
  17. null value handling in hive
  18. null value handling in qlikview
  19. null value handling in sql server
  20. null value handling in sqoop

I am building a site with Bootstrap. On a mobile device, I have a hamburger menu. This appears as part of the Navbar. When I click the navbar, the content of the page is pushed down and the menu ...

I am building a site with Bootstrap. On a mobile device, I have a hamburger menu. This appears as part of the Navbar. When I click the navbar, the content of the page is pushed down and the menu ...

  1. bootstrap navbar horizontal align
  2. bootstrap navbar horizontal links
  3. bootstrap navbar horizontal scroll
  4. bootstrap navbar horizontal scroll mobile
  5. bootstrap navbar horizontal line
  6. bootstrap navbar horizontal to vertical
  7. bootstrap navbar horizontal
  8. bootstrap navbar horizontal list
  9. bootstrap navbar horizontal menu
  10. bootstrap navbar horizontal separator
  11. bootstrap navbar horizontal responsive
  12. bootstrap navbar horizontal center
  13. bootstrap navbar horizontal divider
  14. bootstrap navbar horizontal submenu
  15. bootstrap navbar horizontal dropdown
  16. bootstrap navbar horizontal instead of vertical
  17. bootstrap navbar horizontal collapse
  18. bootstrap horizontal navbar examples
  19. bootstrap horizontal navbar not working
  20. bootstrap 4 navbar horizontal

I'm using a Ruby on Rails application in combination with Materialize CSS. The dropdown of links in the navigation works on the dashboard, but on any other page you have to refresh it first. Same for ...

I'm using a Ruby on Rails application in combination with Materialize CSS. The dropdown of links in the navigation works on the dashboard, but on any other page you have to refresh it first. Same for ...

  1. javascript only loads after refresh

I would like to pass a variable to a specific page. I found a simple example explaining how to use ajax with wordpress. JavaScript: jQuery(document).ready(function($) { // We'll pass this variable ...

I would like to pass a variable to a specific page. I found a simple example explaining how to use ajax with wordpress. JavaScript: jQuery(document).ready(function($) { // We'll pass this variable ...

  1. wordpress send ajax request
  2. wordpress send ajax response
  3. wordpress send ajax
  4. wordpress ajax send form data
  5. wordpress ajax send email
  6. wordpress ajax send data
  7. wordpress ajax send json
  8. wordpress ajax send array
  9. wordpress ajax send mail
  10. wordpress send ajax form