符号&局部变量名不能相同?

时间:2016-09-24 05:03:42

标签: javascript node.js ecmascript-6 es6-class

在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"

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

0 个答案:

没有答案