ES6类/实例属性 [英] ES6 class / instance properties

查看:109
本文介绍了ES6类/实例属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个相对较长的问题,但我真的很想理解。最后问题在问题底部提出。我已经阅读了这个问题的答案:

ES6类变量替代方案



ES6中为什么不接受语法的问题:

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

1)第一个答案提到:


记住,一个类定义定义原型方法 - 在原型上定义
变量一般不是你所做的。


我没有得到这个;基于类的语言中的静态变量似乎与JS中原型上定义的属性具有相同的目的。

显然不是像个人名字一样的实例变量,但它可能是车辆的默认MAX_SPEED ,或所有实例共享的计数器。如果实例不覆盖原型的MAX_SPEED,则返回默认值。这不是静态变量的确切目的吗?



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


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


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



3)关于 ES6的问题的第二个答案类变量选项使我困惑(虽然不是来自技术pov)。


从类方法中可以将该变量作为
this.constructor.foo (或 MyClass.foo )。



这些类属性通常不可访问
类实例。即 MyClass.foo 给出'bar',但新的MyClass()。foo
undefined


这表明很明显可以在类上声明一个类变量(或底层功能),如下例所示: http://www.es6fiddle.net/iehn0hxp/

  class Car {
constructor(){
//设置实例变量
this。 instance_var = 220;
//设置类变量
this.constructor.class_var = 240;
}
}

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

//实例属性
console.log(Mercedes.instance_var); // 220
//类属性
console.log(Car.class_var); // 240

//设置实例属性
Mercedes.instance_var = 120; //我不知道真的:-)
console.log(Mercedes.instance_var); // 120

//类属性
Car.class_var = 140;
console.log(Car.class_var); // 140
//可以从实例
console.log(Mercedes.constructor.class_var)上的constructor属性访问; // 140
console.log(Audi.constructor.class_var); // 140

所以最终它可以声明一个静态财产来自班级;所以我看不到在构造函数中声明它的区别是什么,而不是在类上定义它,而是从外部定义它?最后,它似乎是一个微不足道的技术修改,把它放在构造函数vs作为一个实际的类定义(结果将是一样的)。



真的只是一种设计选择,只能使方法可用吗?

终极问题:



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


我希望我的问题清楚,如果没有,大声喊叫。

解决方案


我没有得到这个;基于类的语言中的静态变量似乎与JS中原型上定义的属性具有相同的目的。


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



这也解答了你的其他问题,我想,但是要概述:




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


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


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




    • 还有一个非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?如果你想要一个类属性,
      //这不是你想要的
      console.log(s.staticProperty); // undefined;它不在实例上
      console.log(Something.staticProperty); // 32;而是在类
      console.log(Something.prototypeProperty); // undefined;这一个不是


      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

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

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

      1) The first answer mentions:

      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?

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

      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 alternatives confuses me (though not from a technical pov).

      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.foo gives 'bar' but new MyClass().foo is undefined

      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/

      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 is possible 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.

      解决方案

      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.

      No, static variables are more like properties defined on the constructor. Variables on the prototype would be closer to 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 in the constructor (instance variables) or assigned to the constructor (class variables)

      • they’re also properties, not variables

      And a non-ES6-sugared example:

      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
      

      这篇关于ES6类/实例属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆