简体   繁体   English

从 Typescript 中的实例访问静态方法

[英]Accessing static methods from instance in Typescript

Why can't I do this?为什么我不能这样做? Is it due to technical limitations of Javascript/Typescript, or is this a design decision by the developers of Typescript?是由于 Javascript/Typescript 的技术限制,还是 Typescript 开发人员的设计决定? This same code would work fine in Java or C#.同样的代码在 Java 或 C# 中也能正常工作。

class Test {
  static str: string = "test";
  public static getTest(): string {
    return this.str;
  }
}

//works as expected
console.log(Test.getTest());
//won't compile
var test: Test = new Test();
console.log(test.getTest());

but I'd still like to know why.但我还是想知道为什么。

Less magic.少魔法。 Static properties exist on the Class not instances.静态属性存在于Class而不是实例上。 Its clear exactly what is getting called from the code instead of magic binding to class or member function at runtime.它很清楚从代码中调用了什么,而不是在运行时魔术绑定到类或成员函数。

Is it due to technical limitations of Javascript/Typescript, or is this a design decision by the developers of Typescript是由于 Javascript/Typescript 的技术限制,还是 Typescript 开发人员的设计决定

Not a technical limitation.不是技术限制。 A design decision.一个设计决策。 More to align with ES6 than anything else : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static But over there its a decision made for less magic与 ES6 保持一致比什么都重要: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static但在那里,它做出了一个不那么神奇的决定

As @basarat says, it's just a design decision, not a technical limitation.正如@basarat 所说,这只是一个设计决定,而不是技术限制。

Actually, even though it's not possible to access test.getTest() just like you would in Java or C#, there are ways to access it:实际上,即使无法像在 Java 或 C# 中那样访问test.getTest() ,也有一些方法可以访问它:

Both Object.getPrototypeOf() (replacement to the now deprecated Object.prototype.__proto__ ) or Object.prototype.constructor should work: Object.getPrototypeOf() (替换为现已弃用的Object.prototype.__proto__ )或Object.prototype.constructor都应该工作:

Object.getPrototypeOf(test).constructor.getTest();

test.constructor.getTest();

Actually:实际上:

Object.getPrototypeOf(test).constructor === test.constructor; // true

Here you can see the compiled source in action:在这里你可以看到编译后的源代码:

 class Test { static getTest() { return this.str; } } Test.str = 'test'; const test = new Test(); console.log(Object.getPrototypeOf(test).constructor.getTest()); console.log(test.constructor.getTest()); console.log(Object.getPrototypeOf(test).constructor === test.constructor); console.log(Object.getPrototypeOf(test) === Test.prototype);

Note static properties exist in classes but not in instances.注意静态属性存在于类中,但不存在于实例中。

Therefore, if you want to go from test to its prototype, you should call Object.getPrototypeOf(test) , not test.prototype , which is a completely different thing.因此,如果你想从test到它的原型,你应该调用Object.getPrototypeOf(test) ,而不是test.prototype ,这是完全不同的事情。

The .prototype property only exists in functions and, when instantiating a new object using new and a call to that constructor function ( new Test() ), will become the newly created object's prototype (the deprecated .__proto__ ). .prototype属性只存在于函数中,当使用new实例化一个新对象并调用该构造函数( new Test() )时,将成为新创建对象的原型(不推荐使用的.__proto__ )。

In your example:在你的例子中:

test.prototype;                     // undefined
Test;                               // class Test { static getTest() { ... } }
Test.protoype;                      // { constructor: class Test { ... } }
Test.protoype.constructor;          // class Test { static getTest() { ... } }
Test.protoype.constructor === Test; // true
test.constructor;                   // class Test { static getTest() { ... } }
test.constructor === Test;          // true

Object.getPrototypeOf(test) === Test.prototype; // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM