问题描述
这是ECMAScript规范2015年6月的代码段。本文不太少的部分。
- 状态由实例承载 - 状态在此上下文中的含义和示例(c ++是首选)
- 方法由类承载 - 这可能意味着如果我想知道对象的方法,我需要查看该对象的类。
- 继承只是结构和行为 - 只有结构和行为是继承的?
- 等等...
任何人都可以详细解释这一点?
好吧,我会回答这个问题。
ECMAScript 6类作为语法糖的背景
ES6类只是在ECMAScript(也称为JavaScript)中发现的普通原型继承中的语法糖。
class Foo {
pre>
constructor(a,b){
this.c = a;
this.d = b;
}
static bar(){}
另一个(){}
}
这相当于:
function Foo(a, b){
this.c = a;
this.d = b;
}
Foo.bar = function(){};
Foo.prototype = {
另一个:function(){}
};
在ES6中继承比旧的方法要容易得多:
class Foo extends Bar {
...
}
在ES5或更早版本中执行相同的操作很容易出现问题,但稍微偏离主题。
/ h2>
类的一个实例是类似于C ++的对象。
class Foo {
constructor(a){
this.value = a;
}
getValue(){
return this.value;
}
}
var foo = new Foo(42);
console.log(foo.getValue()); // print 42
C ++中的等价物:
class Foo {
private:
int value;
public:
Foo(int a):value(a){}
int getValue(){
return value;
}
};
void main(){
Foo foo = new Foo(42);
std :: cout<<< + foo.getValue()<<的std :: ENDL;
}
如您所见,一个实例类在ES6和C ++中的行为方式相同,但是ES6中的
private
或public
封装没有字面上的等价物。在ECMAScript中,您可以覆盖类的实例上的函数,因为它只是一个像其他的对象。 / p>
var foo = new Foo(42);
foo.getValue = function(){return this.value + 1; };
console.log(foo.getValue()); //返回43
console.log(foo.constructor.prototype.getValue.call(foo)); //返回42
使用
.constructor.prototype
新的关键字。这是一个麻烦的措辞,他们有选择。我相信他们的意思是,像ECMAScript一样,类的一个实例只是另外一个对象,你可以修改几乎所有的东西,而在C ++中,一个类的实例有更严格的要求。您无法向实例添加新方法,因此无法添加新属性,您无法打破语言为您提供的限制。
我希望这回答你的问题。如果有什么不清楚,请发表评论,我会进行修改。
This is the snippet from ECMAScript Specification June 2015. I don't understant parts of this text.
- State is carried by instances - what does state mean in this context and example of this (c++ is preferred)
- Methods are carried by classes - this probably means , that if I want to know object's methods , I need to look at the class of that object.
- Inheritance is only of structure and behaviour - only structure and behaviour is inherited?
- And so on...
Can anyone please explain this in details? examples would be great.
Okay, I'll take a shot at answering this question.
Background on ECMAScript 6 classes as syntactic sugar
ES6 classes are just syntactic sugar over the ordinary prototypal inheritance found in ECMAScript (aka JavaScript).
class Foo {
constructor(a, b) {
this.c = a;
this.d = b;
}
static bar() {}
another() {}
}
This is equivalent to:
function Foo(a, b) {
this.c = a;
this.d = b;
}
Foo.bar = function() {};
Foo.prototype = {
another: function() {}
};
Doing inheritance in ES6 is a lot easier than the old method though:
class Foo extends Bar {
...
}
Doing the same in ES5 or earlier is prone to issues, but that is slightly off-topic.
Questions
An instance of a class is an object similar to C++.
class Foo {
constructor(a) {
this.value = a;
}
getValue() {
return this.value;
}
}
var foo = new Foo(42);
console.log(foo.getValue()); // prints 42
Equivalent in C++:
class Foo {
private:
int value;
public:
Foo(int a) : value(a) {}
int getValue() {
return value;
}
};
void main() {
Foo foo = new Foo(42);
std::cout << +foo.getValue() << std::endl;
}
As you can see, the instances of a class behave the same way in ES6 and C++, however there are no literal equivalents to private
or public
encapsulation in ES6.
In ECMAScript, you can override functions on an instance of a class, as it is just an object like everything else.
var foo = new Foo(42);
foo.getValue = function() { return this.value + 1; };
console.log(foo.getValue()); // returns 43
console.log(foo.constructor.prototype.getValue.call(foo)); // returns 42
Using .constructor.prototype
of an object gets the prototype of the object when it has been instantiated with the new
keyword.
It is a cumbersome wording they have chosen. What I believe they mean is that as in ECMAScript, an instance of a class is just another object like anything else, you can modify almost everything about it, while in C++, an instance of a class has stricter requirements. You can't add new methods to an instance, you can't add new properties, and you can't break the constraints that the language has provided for you.
I hope this answers your questions. If anything isn't clear, please comment and I'll edit it.
这篇关于基于类和基于对象的语言比较(ECMAScript规范)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!