lundi 17 juin 2019

js variable throwing different values for different alerts

  • the below code should return 1, 3
  • since x =1 is just present above return statement.
  • but its returning 3,1
  • is there any trick in js variable.
  • will it be same when we use let and constant.
  • it would be great if you guys explain.
  • so that I can make my fundamentals strong
  • providing my code snippet below.
var x = 3;

var foo = {
    x: 2,
    baz: {
        x: 1,
        bar: function() {
            return this.x;
        }
    }
}

var go = foo.baz.bar;

alert(go());
alert(foo.baz.bar());




Aucun commentaire:

Enregistrer un commentaire