我说有,因为你可以使用
var object = new Object()
这样的语法。他说:“因为没有使用
class
这个关键词,所以它不是一个类。”谁是对的?
注意:此问题是在 ECMAScript 第六版(2015年)引入
class
语法 之前提出的。即便如此,正如上面所提到的,仅仅有此语法的存在并不能必然解决问题。var object = new Object()
这样的语法。class
这个关键词,所以它不是一个类。”class
语法 之前提出的。即便如此,正如上面所提到的,仅仅有此语法的存在并不能必然解决问题。Javascript是一种面向对象的编程语言,然而在2015年,通过ECMA script 6引入了类(class),现在可以像其他基于类的语言(如Java)一样使用它们。当然,正如用户codemagician在他/她的评论中指出的那样,JS中的类和Java或其他“基于类”的编程语言的类工作方式存在一些深刻的差异。
尽管如此,在js编程中现在可以使用例如以下代码:
class Animal {
constructor(name) {
this.name = name;
}
class Dog extends Animal {
speak() {
console.log(this.name + ' barks.');
}
}
来源:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
这与经典基于类的语言有些共同之处。问题在于,这项新技术的浏览器支持目前还只是刚刚开始。因此,在生产产品上使用它仍然不太好。但我毫不怀疑,这个问题很快就会得到解决。
因此,问题在于由于实现了这些新特性,js 是否已经成为了一个基于类的编程语言,还是仍然保持着面向对象的原型编程语言。
请在此处收听Douglas Crockford的讲话:
http://developer.yahoo.com/yui/theater/video.php?v=crockonjs-2
他在演讲中直接回答了您的问题:
这种语言最具争议的特点是它进行继承的方式,这与几乎所有其他现代语言都有根本的不同。大多数语言使用类 - 我称之为“经典语言” - 但JavaScript不是这样。JavaScript是无类的。它使用原型。对于那些受过经典培训的人来说,当他们看到这种语言时,他们会认为这很不足。你没有类,你怎么能完成任何事情?你怎么能确信你的程序结构会起作用?他们永远无法超越这一点。但事实证明...
对象
(对象可以从其他对象继承)。它没有传统意义上的类
。尽管可以通过函数原型实现大多数传统类定义/实例化的功能。undefined
、null
等。 - Marcel Korpelvoid
的东西应该是undefined
。 - Yay295来自 You-Dont-Know-JS 书中 https://github.com/getify/You-Dont-Know-JS 的内容
第四章:混合(上)“类”对象
...
JS 已经有了一些类似语法元素(如 new 和 instanceof)相当长的时间,最近在 ES6 中新增了一些关键字,如 class。
但这是否意味着 JavaScript 实际上拥有类?简单明了地说:不是
我不会复制并粘贴其他部分,但鼓励阅读第三章和第四章并运行示例。
https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20%26%20object%20prototypes/ch3.md
https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20%26%20object%20prototypes/ch4.md
当人们说“语言X有类”时,通常指支持面向对象编程。
是的,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
是的。 是的,JavaScript有类和对象。 以下是使用JavaScript/NodeJS类和对象制作区块链的示例:
// coded by Alfrick Opidi in Smashing Magazine blog
// https://www.smashingmagazine.com/2020/02/cryptocurrency-blockchain-node-js/
const SHA256 = require('crypto-js/sha256');
const fs = require('fs')
class CryptoBlock{
constructor(index, timestamp, data, precedingHash=" "){
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.precedingHash = precedingHash;
this.hash = this.computeHash();
}
computeHash(){
return SHA256(this.index + this.precedingHash + this.timestamp + JSON.stringify(this.data)).toString();
}
}
class CryptoBlockchain{
constructor(){
this.blockchain = [this.startGenesisBlock()];
}
startGenesisBlock(){
return new CryptoBlock(0, "01/01/2020", "Initial Block in the Chain, its also called genisis", "0");
}
obtainLatestBlock(){
return this.blockchain[this.blockchain.length - 1];
}
addNewBlock(newBlock){
newBlock.precedingHash = this.obtainLatestBlock().hash;
newBlock.hash = newBlock.computeHash();
this.blockchain.push(newBlock);
}
}
let smashingCoin = new CryptoBlockchain();
smashingCoin.addNewBlock(new CryptoBlock(1, "01/06/2020", {sender: "Iris Ljesnjanin", recipient: "Cosima Mielke", quantity: 50}));
smashingCoin.addNewBlock(new CryptoBlock(2, "01/07/2020", {sender: "Vitaly Friedman", recipient: "Ricardo Gimenes", quantity: 100}) );
fs.writeFile('genisis.json', JSON.stringify(smashingCoin), function (err) {
if (err) throw err;
console.log('Saved!');
});
console.log(JSON.stringify(smashingCoin, null, 4));