JavaScript 有类吗? [英] Does JavaScript have classes?
问题描述
上周我和一个朋友吵架了.他说 JavaScript 中没有类之类的东西.
A friend and I had an argument last week. He stated there were no such things as classes in JavaScript.
我说过正如你所说的 var object = new Object()
他说因为没有使用class
这个词.这不是一个类."
He says "as there is no word class
used. It's not a class."
谁是对的?
作为注释;未来你需要一个简洁的 Classy JS 工具:
As a note; For future you needing a succinct Classy JS implement:
https://github.com/tnhu/jsface
2017 年 7 月
ECMAScript 2015 中引入的 JavaScript 类主要是对 JavaScript 现有的基于原型的继承的语法糖.类语法并未向 JavaScript 引入新的面向对象的继承模型.JavaScript 类提供了一种更简单、更清晰的语法来创建对象和处理继承.
JavaScript classes introduced in ECMAScript 2015 are primarily 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.
- Mozilla ES6 类:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes
- Mozilla ES6 Classes: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes
推荐答案
从技术上讲,JavaScript 没有类"这句话是正确的.
Technically, the statement "JavaScript has no classes" is correct.
虽然 JavaScript 是面向对象的语言,但它不是基于类的语言——它是一种基于原型的语言.这两种方法是有区别的,但是因为 JavaScript 可以像基于类的语言一样使用,所以很多人(包括我自己)经常将构造函数简单地称为类".
Although JavaScript is object-oriented language, it isn't a class-based language—it's a prototype-based language. There are differences between these two approaches, but since it is possible to use JavaScript like a class-based language, many people (including myself) often simply refer to the constructor functions as "classes".
这篇关于JavaScript 有类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!