JavaScript 有类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2752868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:46:15  来源:igfitidea点击:

Does JavaScript have classes?

javascriptoopclass

提问by Glycerine

A friend and I had an argument last week. He stated there were no such things as classes in JavaScript.

上周我和一个朋友吵架了。他说 JavaScript 中没有类之类的东西。

I said there was as you can say var object = new Object()

我说有你可以说的 var object = new Object()

He says "as there is no word classused. It's not a class."

他说“因为没有class使用这个词。这不是一个类。”

Who is right?

谁是对的?



As a note; For future you needing a succinct Classy JS implement:

作为注释;将来你需要一个简洁的 Classy JS 工具:

https://github.com/tnhu/jsface

https://github.com/tnhu/jsface



Edit: July 2017

编辑:2017 年 7 月

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.

ECMAScript 2015 中引入的 JavaScript 类主要是对 JavaScript 现有的基于原型的继承的语法糖。类语法并没有向 JavaScript 引入一个新的面向对象的继承模型。JavaScript 类提供了一种更简单、更清晰的语法来创建对象和处理继承。

- Mozilla ES6 Classes: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes

- Mozilla ES6 类:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes

回答by Steve Harrison

Technically, the statement "JavaScript has no classes" is correct.

从技术上讲,“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 是面向对象的语言,但它不是基于类的语言——它是基于原型的语言。这两种方法之间存在差异,但由于可以像基于类的语言一样使用 JavaScript,因此许多人(包括我自己)通常将构造函数简单地称为“类”。

回答by willy wonka

Javascript is an object oriented programming language, nevertheless in 2015 with ECMA script 6 classes have been introduced and now is correct to use them like other class based languages like Java. Of course as pointed out by the user codemagician in his/her comment, there are some deep differences between how classes work in js and java or other "class based" programming languages.

Javascript 是一种面向对象的编程语言,但在 2015 年 ECMA 脚本中引入了 6 个类,现在可以像 Java 等其他基于类的语言一样使用它们。当然,正如用户代码魔术师在他/她的评论中指出的那样,类在 js 和 java 或其他“基于类”的编程语言中的工作方式之间存在一些深刻的差异。

Nevertheless now in js programming it is possible to use for example code like:

不过现在在 js 编程中可以使用以下示例代码:

class Animal { 
  constructor(name) {
    this.name = name;
  }


class Dog extends Animal {
  speak() {
    console.log(this.name + ' barks.');
  }
}

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

来源:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

That has something in common with classical class-based languages. The problems still is the browser support of this new technology that is just at start at the moment. So it still is not good to use it on productions products. But I don't have any doubt that this issue is going to be solved fast.

这与经典的基于类的语言有一些共同之处。问题仍然是这种新技术的浏览器支持,目前才刚刚开始。所以在生产产品上使用它仍然不好。但我毫不怀疑这个问题会很快得到解决。

Hence the question remains if js has become a class-based programming language because of the implementation of this new features or does it still remain an object prototyping oriented programming language.

因此,问题仍然存在,js 是否因为实现了这个新特性而成为一种基于类的编程语言,或者它仍然是一种面向对象原型的编程语言。

回答by ChristopheD

In Javascript pretty much everything is an object(objects can inherit from other objects). It does not have classesin the classical sense.

在 Javascript 中几乎所有东西都是一个object(对象可以从其他对象继承)。它没有classes经典意义上的。

Although you can reproduce most of the functionality of traditional class definition / instantiation by function prototyping.

虽然您可以通过函数原型来重现传统类定义/实例化的大部分功能。

回答by Christopher Altman

Listen to Douglas Crockford's talk here:
http://developer.yahoo.com/yui/theater/video.php?v=crockonjs-2

在这里听道格拉斯·克罗克福德 (Douglas Crockford) 的演讲:http: //developer.yahoo.com/yui/theater/video.php?v=
crockonjs-2

He directly addresses your question in his presentation:

他在演讲中直接回答了你的问题:

The most controversial feature of the language is the way it does inheritance, which is radically different than virtually all other modern languages. Most languages use classes – I call them ‘classical languages' – JavaScript does not. JavaScript is class free. It uses prototypes. For people who are classically trained who look at the language, they go: well, this is deficient. You don't have classes, how can you get anything done? How can you have any confidence that the structure of your program's going to work? And they never get past that. But it turns out…

该语言最具争议的特征是它进行继承的方式,这与几乎所有其他现代语言完全不同。大多数语言都使用类——我称它们为“经典语言”——JavaScript 没有。JavaScript 是类免费的。它使用原型。对于那些受过经典训练的人来说,他们会看语言,他们会说:嗯,这是有缺陷的。你没有课,你怎么做事?你怎么能相信你的程序结构会起作用?他们永远不会超越这一点。但事实证明……

回答by Pavel Radzivilovsky

By "language X has classes" people usually mean support of object oriented programming.

人们所说的“X 语言有类”通常是指支持面向对象编程。

Yes, Javascript is an object oriented language.

是的,Javascript 是一种面向对象的语言。

回答by Alireza Fattahi

From You-Dont-Know-JSbook at https://github.com/getify/You-Dont-Know-JS

来自https://github.com/getify/You-Dont-Know-JS 的You-Dont-Know-JS

Chapter 4: Mixing (Up) "Class" Objects

第 4 章:混合(上)“类”对象

...

JS has had some class-like syntactic elements (like new and instanceof) for quite awhile, and more recently in ES6, some additions, like the class keyword.

But does that mean JavaScript actually has classes? Plain and simple: No

...

JS 已经有一些类似类的语法元素(比如 new 和 instanceof)有一段时间了,最​​近在 ES6 中增加了一些,比如 class 关键字。

但这是否意味着 JavaScript 实际上有类?简单明了:没有

I am not going to copy and past other parts here but encourage to read chapter 3& chapter 4and run samples.

我不打算复制和过去在这里等部位,但建议你阅读第3章第四章和运行样品。

https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch3.mdhttps://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch4.md

https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch3.md https://github.com/getify/You-Dont-Know -JS/blob/master/this%20%26%20object%20prototypes/ch4.md

回答by fthinker

When I think of classes I think of types and the fact that classes allow me to define new types. In js you can't create new types. You can do all sorts of fancy oo stuff with prototypes but the fact that everything is still an object really hits home the class-less nature of js. I think that people using 'class' terminology when talking about js confuses the js as a prototype language vs js as a classical language even more than the ugly new operator. In short, just because js is OO doesn't imply that classes need to exist.

当我想到类时,我会想到类型以及类允许我定义新类型的事实。在 js 中你不能创建新类型。你可以用原型做各种花哨的 oo 东西,但一切仍然是一个对象的事实确实让 js 的无类性质成为了现实。我认为人们在谈论 js 时使用“类”术语,将 js 作为原型语言与作为经典语言的 js 混淆,甚至比丑陋的新运算符更容易混淆。简而言之,仅仅因为 js 是面向对象的并不意味着类需要存在。

回答by James Drinkard

To add in with the other answers, javascript does not have classes, although I'm starting to see statements where it is described as something like classes, but I believe that just confuses the issue.

要添加其他答案,javascript 没有类,尽管我开始看到将其描述为类之类的语句,但我相信这只会混淆问题。

JavaScript has prototypes, not classes, but they accomplish the same thing, prototypes are objects that define objects, hence the confusion.

JavaScript 有原型,而不是类,但它们完成相同的事情,原型是定义对象的对象,因此混淆。

A prototype is a representation of private internal state that a class would manage in Java for example. Instead of putting that internal state in a class and presenting an interface for manipulating behaviour, as in java, JavaScript exposes the data structure for JavaScript programs to manipulate directly.

例如,原型是类将在 Java 中管理的私有内部状态的表示。不像在 Java 中那样将内部状态放在一个类中并提供一个用于操作行为的接口,JavaScript 公开了 JavaScript 程序可以直接操作的数据结构。

This is the best description I've found on the subject, Prototypes are not Classes.

这是我在这个主题上找到的最好的描述,Prototypes 不是 Classes

回答by Puneet Lamba

Although JavaScript didn't have classes prior to ES6, class-like behavior could be implemented in ES5 by sealing objects (thereby making objects non-extensible). In a sealed object, new properties and methods cannot be added and properties are not configurable. Property valuescan still be set and read. I say class-like, because there's one caveat. A sealed object's method definitions can still be modified. That's because property values can still be set, unless you change all method properties to be non-writeable -- at which point you've reproduced class behavior pretty closely using ES5.

尽管 JavaScript 在 ES6 之前没有类,但在 ES5 中可以通过密封对象(从而使对象不可扩展)来实现类的行为。在密封的对象中,不能添加新的属性和方法,并且属性是不可配置的。仍然可以设置和读取属性。我说类,因为有一个警告。密封对象的方法定义仍然可以修改。这是因为仍然可以设置属性值,除非您将所有方法属性更改为不可写——此时您已经使用 ES5 非常接近地重现了类行为。

回答by Mandeep Kaur

In simple words - Yes. All you need is Babel.js transpiler, because all browsers does not support it except Chrome browser. A JavaScript class is a type of function. Classes are declared with the class keyword. We use function expression syntax to initialize a function and class expression syntax to initialize a class.

简单来说 - 是的。你只需要 Babel.js 转译器,因为除 Chrome 浏览器外,所有浏览器都不支持它。JavaScript 类是一种函数。类是用 class 关键字声明的。我们使用函数表达式语法来初始化一个函数和类表达式语法来初始化一个类。

Here is an example of JavaScript class using function:

下面是一个使用函数的 JavaScript 类示例:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100