ECMAScript 是否在其第 6 版中停止了基于原型的编程语法?
Has ECMAScript discontinued prototye-based programing syntax in its 6th edition?
我是 JavaScript 的 ECMAScript5 语法的忠实粉丝,主要是因为,在我看来,正是它使它在遵循相对传统的编程风格和语法的其他面向对象编程语言中独树一帜。
不确定 ECMAScript6 是否仍支持 ECMAScript5 语法,即它是否向后兼容。但是,是否有任何理由替换语法,尤其是 基于原型的 在 JavaScript 中定义 classes 的方法(更不用说继承等)变成更传统的语言(如大多数面向对象的编程语言)?
为了举例说明语法更改,根据 ECMAScript5,您可以在 JavaScript:
中定义一个人 class
// ES5 class definition of a Person
function Person (name) {
this.name = name;
}
//adding methods or properties to the Person prototype
Person.prototype.walk = function(){
console.log(this.name + ' is walking');
}
上面在 ES6 中的相同实现:
class Person {
constructor(name) {
this.name = name;
}
walk() {
console.log(this.name + ' is walking.');
}
}
我问这个问题,因为我相信,对于一些程序员来说,由于太习惯于旧的风格,一开始可能转回传统语法被证明是有问题的。那么为什么首先会带来如此剧烈的变化呢?
非常感谢任何信息。
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.
我是 JavaScript 的 ECMAScript5 语法的忠实粉丝,主要是因为,在我看来,正是它使它在遵循相对传统的编程风格和语法的其他面向对象编程语言中独树一帜。
不确定 ECMAScript6 是否仍支持 ECMAScript5 语法,即它是否向后兼容。但是,是否有任何理由替换语法,尤其是 基于原型的 在 JavaScript 中定义 classes 的方法(更不用说继承等)变成更传统的语言(如大多数面向对象的编程语言)?
为了举例说明语法更改,根据 ECMAScript5,您可以在 JavaScript:
中定义一个人 class// ES5 class definition of a Person
function Person (name) {
this.name = name;
}
//adding methods or properties to the Person prototype
Person.prototype.walk = function(){
console.log(this.name + ' is walking');
}
上面在 ES6 中的相同实现:
class Person {
constructor(name) {
this.name = name;
}
walk() {
console.log(this.name + ' is walking.');
}
}
我问这个问题,因为我相信,对于一些程序员来说,由于太习惯于旧的风格,一开始可能转回传统语法被证明是有问题的。那么为什么首先会带来如此剧烈的变化呢?
非常感谢任何信息。
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.