简体   繁体   English

为什么ES6类中无法实现本地功能?

[英]Why are local functions not possible in ES6 classes?

What was the idea about leaving out something like this in ES6: 在ES6中遗漏了类似的东西是什么想法:

class Foo {
   myMethod(){
      // do something with 'bar'
   }
   constructor(){
       this.myMethod();
   }
   otherMethod(){
       this.myMethod();
   }
}

I know that it's possible to define the function in the constructor or outside the class and then use it with myMethod() . 我知道可以在构造函数中或类外部定义函数,然后将其与myMethod() However coming from other languages I was surprised to see classes, but no local (or private) methods. 然而,来自其他语言我很惊讶地看到了类,但没有本地(或私有)方法。 I couldn't find anything on the internet about the reason why this was left out. 我在互联网上找不到关于这个被遗漏的原因。

EDIT: I just realized your post was about functions, not variables. 编辑:我刚刚意识到你的帖子是关于函数,而不是变量。 Since functions are a type of variable, all these solutions work for function even though I didn't explicitly make the examples functions 由于函数是一种变量,所有这些解决方案都适用于函数,即使我没有明确地生成示例函数


I have found several solutions, each of which have their pros and cons: 我找到了几个解决方案,每个解决方案各有利弊:

Method 0: Factories 方法0:工厂

 var Foo = (function() { let priv = { "eh": 0 }; return class Foo { constructor(num) { priv.eh = num; } test() { return priv.eh; } }; })(); var a = new Foo(383); console.log(a.test()); 

Taking advantage of JS scoping to hide a variable, priv behind a function 利用JS作用域隐藏变量, priv在函数后面

Pros: 优点:

  • Perfectly secure. 完全安全。 It's basically impossible to access priv unless you return a pointer of it 除非你返回它的指针,否则基本上不可能访问priv
  • Takes advantage of a well-used javascript paradigm. 利用一个很好用的javascript范例。 Factories have been used by programmers for year 工程师已经被程序员使用了一年
    • Private variables do not clash with parent class(es) 私有变量不会与父类冲突

Cons: 缺点:

  • It's not very clear or readable 它不是很清晰或可读

Method 1: Define everything from the constructor 方法1:从构造函数定义所有内容

 class Foo2 { constructor(num) { Object.assign(this, { test() { return num; } }); } } var b = new Foo2(262); console.log(b.test()); 

Just what it says on the box. 正如它在盒子上所说的那样。

Pros: 优点:

  • Perfectly secure again. 再次完全安全。 There's no way to access local variable from outside the scope 无法从范围外访问局部变量
  • Little more readable than Method 1. It's somewhat obvious what everything does 比方法1更具可读性。一切都做得有些明显
  • Private variables do not clash with parent class(es) 私有变量不会与父类冲突

Cons: 缺点:

  • Clogs the constructor function 阻塞构造函数
  • Still not very readable 仍然不是很可读

Method 2: Naming Convention 方法2:命名约定

 class Foo3 { constructor(num) { this._eh = num; } test() { return this._eh; } } var c = new Foo3(101); console.log(c.test()); 

No need to hide behind strange security procedures. 无需隐藏在奇怪的安全程序背后。 Just specify in the name which properties are "private" 只需在名称中指定哪些属性为“私有”

Pros: 优点:

  • Very easy to read, can take advantage of class structure 很容易阅读,可以利用类结构

Cons: 缺点:

  • Offers absolutely no protection, only a recommendation. 提供绝对没有保护,只有一个建议。
  • Private variables clash with the private variables of parent classes 私有变量与父类的私有变量冲突

Method 3: Symbols 方法3:符号

 const eh = Symbol("eh"); class Foo4 { constructor(num) { this[eh] = num; } test() { return this[eh]; } } var d = new Foo4(100); console.log(d.test()); 

I just wanted to include this one because I thought it was cool 我只是想包括这个,因为我觉得它很酷

Pros: 优点:

  • Readable as naming convention. 可读作为命名约定。 Private variables don't get strings, they get Symbols. 私有变量没有字符串,他们得到符号。 Very easy to read 很容易阅读

Cons: 缺点:

  • Pointless. 无意义。 In any situation where the parent scope is protected, you can just store private variables there 在父作用域受到保护的任何情况下,您只需将私有变量存储在那里
    • Not secure. 不安全。 Even if you solve the problem above, people can access all keys (including Symbols) with Reflect.ownKeys() 即使您解决了上述问题,人们也可以使用Reflect.ownKeys()访问所有键(包括符号Reflect.ownKeys()

Hope this was helpful! 希望这有用!

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

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