// ES6 class Person{ constructor(name, age){ this.name = name; this.age = age; } getName(){ console.log(this.name) } } p = new Person('alex', 34); p.getName(); // ES5 function Person(name){ this.name = name; this.getName = function(){ console.log(this.name) } } var p = new Person('tom'); p.getName(); //ES5 var person = { name: 'tom', getName: function(){ console.log(this.name); } } person.getName();
ES5与ES6对比:Person类实现
本文通过对比ES5和ES6两种不同语法风格下Person类的实现方式,展示了ES6类语法的简洁性和面向对象特性。分别用ES5的构造函数和对象字面量方式以及ES6的class关键字创建Person实例,并调用getName方法。

被折叠的 条评论
为什么被折叠?



