es6 propertyIsEnumerable
es6 propertyIsEnumerable
Public 通过 es6 class 语法创建的方法不可枚举。用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 class 语法不会那样做。
要(部分)复制 ES5 中的 ES2015 功能,您必须使用 Object.defineProperty(Cat.prototype, 'getname', ...)
添加 getname
方法
正如你所说,ES6 class 方法是不可枚举的。另一个区别是它们不可构造:
new cat.__proto__.getname; // works
new dog.__proto__.getname; // throws a TypeError
此外,class 中的方法代码始终是 strict mode code and can take advantage of certain language features like the super
关键字。
Public 通过 es6 class 语法创建的方法不可枚举。用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 class 语法不会那样做。
要(部分)复制 ES5 中的 ES2015 功能,您必须使用 Object.defineProperty(Cat.prototype, 'getname', ...)
getname
方法
正如你所说,ES6 class 方法是不可枚举的。另一个区别是它们不可构造:
new cat.__proto__.getname; // works
new dog.__proto__.getname; // throws a TypeError
此外,class 中的方法代码始终是 strict mode code and can take advantage of certain language features like the super
关键字。