class与prototype
创建实例对象:
ES5中常用的构造函数模式
function Person(name){ this.name = name; this.getName = function(){ return this.name }}
ES6 通过class定义类
class Person{ constructor(name){ this.name = name; } getName(){ return this.name; };}
1、类的所有方法都定义在类的prototype属性上面
2、类和原型上的方法、属性都是不可枚举的,所以只能通过Object.getOwnPropertyNames(Person/Person.prototype)获取其属性、方法名来遍历;
注:1)for-in, Object.keys(), Object.getOwnPropertyNames三种方式遍历对象
2)for-in和Object.keys()只能获取可枚举的属性、方法
参考:
http://es6.ruanyifeng.com/#docs/class
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。