是否有一个在ES6中命名符号的约定? [英] Is there a convention for naming symbols in ES6?

查看:133
本文介绍了是否有一个在ES6中命名符号的约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和ES6一起玩耍,看着符号。与ruby不同,例如您要写入:symbol ,ES6符号似乎允许任何标准变量名称。说实话,我发现这很混乱:

  var privateProperty = Symbol(); 
var obj = {};
obj [privateProperty] ='某些值在这里';

因为它往往使我认为 privateProperty 可能是像过去几年一样的简单字符串。使用:privateProperty 无效。



所以就像使用 $ bar 为一个jQuery对象或$ code> bar $ 为RxJS /培根流,我想知道是否已经有一个已建立的惯例命名符号在ES6?

解决方案


obj [privateProperty] = ...; 让我觉得 privateProperty 可能是一个像过去几年一样的纯字符串。




<那么这就是我们需要忘记的旧思维。 privateProperty 是一个 ,可以是字符串(属性名称)或符号。同样,我们已经学会了从正常属性中区分(整数)索引


ES6似乎允许任何标准变量名称


与Ruby一样。不同之处在于,ES6不会为符号引入字面符号(它解决相同的事情),而是允许它们仅通过使用符号(本地)或(全球)。



命名变量没有标准约定它拥有符号值。

当然,您可以随时使用匈牙利语符号如果你想要这样的类型注释。如果我不得不投入一个标准,我建议使用一个更微妙的领先下划线( var _myPrivate )。财产名称中的下划线一直暗示着一些特殊的东西,它的计算属性键( obj [_myPrivate] )看起来会相似。


有没有在ES6中命名符号的约定?


虽然没有约定对于命名符号变量(还有),当然有一个命名符号本身的约定:




  • 本地符号应该有一个非常描述性的描述符字符串(作为符号的参数给出)。当然,它们仍然是独一无二的,所以你基本上可以使用你想要的任何名字。

  • 全局符号需要避免碰撞。约定是适当地命名它们,并将这些命名空间名称与点连接 - 如Java包名称。如果符号可用作全局变量,则其描述符应具有相同的名称(类似于内置的本机符号,它们像 @@ iterator = Symbol.for(Symbol.iterator); )。如果符号由模块或库导出,则其描述符应以模块/库名称为前缀(以避免冲突)。



对于变量使用相同的名称可能是最好的做法(就像本地符号已经做的那样, Symbol.iterator.toString()==Symbol(Symbol.iterator))。


I'm toying around with ES6, looking at symbols. Unlike ruby for example where you'd write :symbol, ES6 symbols seem to be allowed any "standard" variable name. To be honest I am finding this rather confusing:

var privateProperty = Symbol();
var obj = {};
obj[privateProperty] = 'some value goes here';

as it tends to make me think that privateProperty is probably a plain string like the years before. Using :privateProperty is not valid.

So just like using $bar for a jQuery object or bar$ for a RxJS/Bacon stream, I was wondering if there is already an established convention for naming symbols in ES6?

解决方案

obj[privateProperty] = …; tends to make me think that privateProperty is probably a plain string like the years before.

Well, that's the old thinking that we will need to forget. privateProperty is a key, which can be either a string (property name) or symbol. Similarly, we already have learned to distinguish (integer) indices from "normal" properties.

ES6 seems to allow any "standard" variable name

Just as does Ruby. The difference is that ES6 doesn't introduce a literal notation for symbols (which resolve to the same thing), but allows them to be created only by using Symbol (locally) or Symbol.for (globally).

There is no standard convention for naming variables that hold symbol values.
Of course, you can always use hungarian notation if you tend to want such type annotations. If I had to coin a standard, I'd propose to use a more subtle leading underscore (var _myPrivate). Underscores in property names had always implied something special, and it would look similar for computed property keys (obj[_myPrivate]) then.

Is there a convention for naming symbols in ES6?

While there is no convention for naming symbol-holding variables (yet), there certainly is a convention for naming symbols themselves:

  • local symbols should have a very descriptive descriptor string (given as an argument to Symbol). Of course, they're still unique anyway, so you basically can use whatever name you want.
  • global symbols need to avoid collisions. The convention is to namespace them appropriately, and connect those namespace names with dots - like a Java package name. If the symbol is available as a global variable, its descriptor should have the same name (similar to the builtin, native symbols, which are like @@iterator = Symbol.for("Symbol.iterator");). If the symbol is exported by a module or library, its descriptor should be prefixed with the module/library name (to avoid collisions).

And it would probably be a best practise to use the same name for the variable (like the native symbols already do, Symbol.iterator.toString() == "Symbol(Symbol.iterator)").

这篇关于是否有一个在ES6中命名符号的约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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