0

在 ES6 中全局符号和局部变量是冲突的。这里是代码

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"

为什么会这样?这是一个错误吗?

4

0 回答 0