javascript ES6 类/实例属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32545495/
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-10-28 15:26:56  来源:igfitidea点击:

ES6 class / instance properties

javascriptecmascript-6

提问by Trace

This is going to be a relatively long question, but one I would really like to understand. Final question formulated at the bottom of the question.

这将是一个相对较长的问题,但我真的很想了解。在问题的底部制定了最终问题。

I have read the answers to this question:
ES6 class variable alternatives

我已经阅读了这个问题的答案:
ES6 类变量替代

The question on why this is not accepted syntax in ES6:

关于为什么这在 ES6 中不被接受的语法的问题:

class MyClass {
    const MY_CONST = 'string';
    constructor(){
        this.MY_CONST;
    }
}

1) The first answer mentions:

1)第一个答案提到:

Remember, a class definition defines prototype methods - defining variables on the prototype is generally not something you do.

请记住,类定义定义了原型方法——在原型上定义变量通常不是您要做的。

I don't get this; static variables in a class based language would appear to serve the same purpose as properties defined on the prototype in JS.
Obviously not an instance variable like a person's name, but it could be a default MAX_SPEED for a vehicle, or a counter that is shared by all instances. If the instance doesn't override the prototype's MAX_SPEED, it returns to the default. Isn't that the exact purpose of a static variable?

我不明白;基于类的语言中的静态变量似乎与 JS 中原型上定义的属性具有相同的目的。
显然不是像人名这样的实例变量,但它可以是车辆的默认 MAX_SPEED,或者是所有实例共享的计数器。如果实例没有覆盖原型的 MAX_SPEED,它会返回到默认值。这不是静态变量的确切用途吗?

2) The following post(ES6 spec proposal) formulates:

2)以下帖子(ES6规范提案)制定:

There is (intentionally) no direct declarative way to define either prototype data properties (other than methods) class properties, or instance property. Class properties and prototype data properties need be created outside the declaration.

(有意)没有直接的声明方式来定义原型数据属性(方法除外)类属性或实例属性。类属性和原型数据属性需要在声明之外创建。

I don't see the actual difference in declaring / initialising an instance/class variable with default value within the class itself (outside the constructor)? What does it matter if it's on the prototype? If it concerns an instance variable with a default value that will be likely for all instances (yet overridable), I don't see what is the problem. So what is this intention about exactly?

我没有看到在类本身(在构造函数之外)声明/初始化具有默认值的实例/类变量的实际区别?如果它在原型上有什么关系?如果它涉及一个具有默认值的实例变量,该值可能适用于所有实例(但可覆盖),我看不出有什么问题。那么这个意图究竟是什么?

3) The second answer on the question ES6 class variable alternativesconfuses me (though not from a technical pov).

3)关于ES6 类变量替代问题的第二个答案让我感到困惑(尽管不是来自技术观点)。

From within a class method that variable can be accessed as this.constructor.foo(or MyClass.foo).

These class properties would not usually be accessible from to the class instance. i.e. MyClass.foogives 'bar' but new MyClass().foois undefined

从一个类方法中,该变量可以作为this.constructor.foo(或MyClass.foo)访问 。

这些类属性通常不能从类实例访问。即MyClass.foo给出 'bar' 但是new MyClass().fooundefined

This indicates that it is clearly possible to declare a class variable on the class (or underlying function) as implemented in this example: http://www.es6fiddle.net/iehn0hxp/

这表明显然可以在此示例中实现的类(或底层函数)上声明类变量:http: //www.es6fiddle.net/iehn0hxp/

class Car{
  constructor(){
    //Set instance variable 
    this.instance_var = 220; 
    //Set class variable 
    this.constructor.class_var = 240; 
  }
}

var Mercedes = new Car(); 
var Audi = new Car(); 

//Instance property 
console.log(Mercedes.instance_var); //220 
//Class property 
console.log(Car.class_var); //240

//Set instance property 
Mercedes.instance_var = 120; //Well I don't know really :-) 
console.log(Mercedes.instance_var); //120 

//Class property 
Car.class_var = 140; 
console.log(Car.class_var); //140 
//Can be accessed from the constructor property on the instance
console.log(Mercedes.constructor.class_var); //140 
console.log(Audi.constructor.class_var); //140 

So in the end it ispossible to declare a static property from within the class; so I don't see what is the difference declaring it within the constructor, versus just defining it on the class, vs defining it from outside? In the end it just seems to be a trivial technical modification to put it in the constructor vs as an actual class definition (the result will be the same).

那么到底它可以从类中声明静态属性; 所以我看不出在构造函数中声明它与仅在类上定义它与从外部定义它有什么区别?最后,将它放在构造函数中而不是作为实际的类定义(结果将是相同的)似乎只是一个微不足道的技术修改。

Is it really just a design choice to only make methods available?
Ultimate question:

仅提供方法是否真的只是一种设计选择?
终极问题:

Because I don't understand how being a prototype-language changes the philosophy of having properties on the prototype against static variables on a class. It looks the same to me.

因为我不明白作为原型语言如何改变原型上具有属性而不是类上的静态变量的哲学。对我来说看起来一样。

I hope that my question is clear, shout if not.

我希望我的问题很清楚,如果没有就喊出来。

采纳答案by Ry-

I don't get this; static variables in a class based language would appear to serve the same purpose as properties defined on the prototype in JS.

我不明白;基于类的语言中的静态变量似乎与 JS 中原型上定义的属性具有相同的目的。

No, static variables are more like properties defined on the constructor. Variables on the prototype would be closerto instance variables, but they're not nearly as useful because they're shared between instances. (So if you modify a mutable property on the prototype, it will be reflected in all other instances of that type.)

不,静态变量更像是在构造函数上定义的属性。原型上的变量更接近于实例变量,但它们几乎没有那么有用,因为它们在实例之间共享。(因此,如果您修改原型上的可变属性,它将反映在该类型的所有其他实例中。)

This also answers your other questions, I think, but to recap:

我认为,这也回答了您的其他问题,但要回顾一下:

  • variables on the prototype are not like static variables in that they appear to belong to every instance rather than just the class

  • variables on the prototype are not like instance variables in that each instance of the class doesn't have its own instance of the variable

  • therefore, variables on the prototype are not that useful and they should be assigned to inthe constructor (instance variables) or assigned tothe constructor (class variables)

  • they're also properties, not variables

  • 原型上的变量不像静态变量,因为它们似乎属于每个实例而不仅仅是类

  • 原型上的变量不像实例变量,因为类的每个实例都没有自己的变量实例

  • 因此,原型上的变量不是那么有用,它们应该构造函数中分配(实例变量)或分配构造函数(类变量)

  • 它们也是属性,而不是变量

And a non-ES6-sugared example:

还有一个非 ES6 加糖的例子:

function Something() {
    this.instanceProperty = 5;
}

Something.staticProperty = 32;

Something.prototype.prototypeProperty = 977;

var s = new Something();
console.log(s.instanceProperty); // 5
console.log(s.prototypeProperty); // 977? If you want a class property,
                                  // this is not what you want
console.log(s.staticProperty); // undefined; it's not on the instance
console.log(Something.staticProperty); // 32; rather, it's on the class
console.log(Something.prototypeProperty); // undefined; this one isn't