通过 es6 class 语法创建的公共(public)方法是不可枚举的。用 es5 和 es6 编写的 'getname' 方法有什么区别?
function Cat(){
this.name="cat"
}
Cat.prototype.getname = function() {return this.name}
var cat = new Cat()
class Dog {
constructor(){
this.name="dog"
}
getname() {
return this.name
}
}
var dog = new Dog()
cat.__proto__.propertyIsEnumerable("getname") //true
dog.__proto__.propertyIsEnumerable("getname") //false
最佳答案
使用 obj.property = foo
将属性直接添加到对象将始终创建可枚举属性。
ES2015 类语法不会那样做。
要(部分)复制 ES5 中的 ES2015 功能,您必须使用 Object.defineProperty(Cat.prototype, 'getname', ...)
getname
添加方法
关于javascript - es6 propertyIsEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41875897/