0

In ES6 global symbol and local variable are conflicting.Here is the code

var firstName = Symbol();
var lastName = Symbol();

class Person {

constructor(firstName, lastName){
    this[firstName] = firstName;
    this[lastName] = lastName;
    console.log(this[firstName] , this[lastName]);
}
greet(name){
    return "hello " + name + ", I am " + this[firstName];
}
}
var obj1 = new Person("john","Doe");
console.log(obj1.greet("khaled"));//outputs "hello khaled, I am undefined"

why does this happens? is it a bug?

  • Ummm....use different variable names? if you used a linter, you would probably see a message saying that variable is shadowed. –  Sep 24 '16 at 05:10
  • 2
    This is standard javascript and generally called shadowing. Formal parameter names shadow variables of the same name in outer function or global scope. See this [answer](http://stackoverflow.com/questions/5373278/variable-shadowing-in-javascript#5373899) as well. – traktor Sep 24 '16 at 05:24
  • 1
    It's not a bug. It's how scope works in JavaScript. It has nothing to do with symbols in particular. – Felix Kling Sep 24 '16 at 16:45

0 Answers0