为什么命名空间从 ECMAScript 考虑中删除?

发布于 2024-08-24 19:28:41 字数 894 浏览 5 评论 0原文

命名空间曾经是 ECMAScript(旧的 ECMAScript 4)的考虑因素,但已被删除。正如 Brendan Eich 在此消息中所说:

命名空间的用例之一 ES4 是早期绑定(使用命名空间 内在),无论是性能还是 为了程序员的理解——不 运行时名称绑定的机会 不同意之前的任何观点 绑定。但任何早期绑定 动态代码加载场景如 网络需要优先级或 预约机制,避免提早 与后期绑定冲突。

另外,正如一些 JS 实现者所言 关注,多开
命名空间会施加运行时成本,除非 实施有效
难度明显加大。

由于这些原因,命名空间和 早期绑定(如之前的包
他们,去年四月)必须离开。

但我不确定我是否理解所有这些。到底什么是优先级或预留机制?为什么需要其中任何一个?另外,早期绑定和命名空间必须齐头并进吗?由于某种原因,我无法全神贯注于所涉及的问题。有人可以尝试更充实的解释吗?

另外,为什么命名空间会带来运行时成本?在我看来,我不禁发现命名空间和使用闭包的函数之间的概念几乎没有什么区别。例如,Yahoo 和 Google 都拥有 YAHOO 和 google 对象,它们的“作用类似于”名称空间,因为它们在单个访问点中包含所有公共和私有变量、函数和对象。那么,为什么命名空间在实现上会有如此显着的不同呢?也许我只是对命名空间到底是什么有一个误解。

为了赏金,我想知道两件事:

  1. 命名空间是否需要早期绑定?
  2. 命名空间如何实现 与一个物体不同 私人会员?

Namespaces were once a consideration for ECMAScript (the old ECMAScript 4) but were taken out. As Brendan Eich says in this message:

One of the use-cases for namespaces in
ES4 was early binding (use namespace
intrinsic), both for performance and
for programmer comprehension -- no
chance of runtime name binding
disagreeing with any earlier
binding. But early binding in any
dynamic code loading scenario like
the web requires a prioritization or
reservation mechanism to avoid early
versus late binding conflicts.

Plus, as some JS implementors have
noted with concern, multiple open
namespaces impose runtime cost unless
an implementation works
significantly harder.

For these reasons, namespaces and
early binding (like packages before
them, this past April) must go.

But I'm not sure I understand all of that. What exactly is a prioritization or reservation mechanism and why would either of those be needed? Also, must early binding and namespaces go hand-in-hand? For some reason I can't wrap my head around the issues involved. Can anyone attempt a more fleshed out explanation?

Also, why would namespaces impose runtime costs? In my mind I can't help but see little difference in concept between a namespace and a function using closures. For instance, Yahoo and Google both have YAHOO and google objects that "act like" namespaces in that they contain all of their public and private variables, functions, and objects within a single access point. So why, then, would a namespace be so significantly different in implementation? Maybe I just have a misconception as to what a namespace is exactly.

For the sake of the bounty I'd like to know two things:

  1. Does a namespace need early binding?
  2. How does a namespace implementation
    differ from an object with
    private members?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-08-31 19:28:41

如果在函数定义调用该变量之后在闭包中声明变量,它仍然使用作用域变量。

function ShowMe() { 
    alert(myVar); //alerts "cool"
}

var myVar = "cool";

这个过程在命名空间方面会变得更加复杂。

除此之外,还有许多命名空间方法以及 Expand/applyIf 等可以执行许多相同的功能。例如,ExtJS 中的namespace() 或jQuery 中的$.extend。因此,拥有它可能是件好事,但在语言结构中并不是绝对需要。我认为形式化数组的一些扩展以及 Date 中对 ISO-8601 日期的支持对我自己来说更为重要。不必简单地检查每个命名空间层的定义......

window.localization = $.extend(window.localization || {}, {
  ...
});

window.localization.en = $.extend(window.localization.en || {}, {
  ...
});

If you declare a variable within a closure after a definition of a function will call that variable, it still uses the scoped variable.

function ShowMe() { 
    alert(myVar); //alerts "cool"
}

var myVar = "cool";

This process would get another layer of complicated with regards to namespacing.

Beyond this there are numerous namespace methods along with expand/applyIf etc that can do a lot of the same functionality. namespace() in ExtJS, or $.extend in jQuery for example. So, it may be a nice to have, but isn't an absolute need within the constructs of the language. I think that formalizing some of the extensions for Array, and support for ISO-8601 dates in Date are far more important myself. Over having to simply check each namespace layer for definition...

window.localization = $.extend(window.localization || {}, {
  ...
});

window.localization.en = $.extend(window.localization.en || {}, {
  ...
});
弃爱 2024-08-31 19:28:41

首先确定 一些术语:

  • 早期绑定——在解析代码行时进行检查/验证。
  • 后期绑定——在执行代码行时检查/验证。
  • 优先级/保留——我认为它们的意思是,如果您进行早期绑定并在解析代码时检查名称空间,则必须有一个过程来在稍后动态添加变量时验证变量名称,并且必须有一组不同的规则是有效的,因为此时可能涉及多个范围。

坦率地说,我很惊讶只给出了这些相当技术性的原因。以常见情况为例:-

onclick=' var mymark = "donethat";'

“mymark”应该属于哪个命名空间?这个问题似乎没有任何简单的答案。特别是考虑到代码片段:-

window.forms.myform.mybutton.onClick = ' var mymark = "donethat";'

应该将变量放在相同的名称空间中。

OK first Some of the terminology:

  • Early binding -- check/validate when the line of code is parsed.
  • Late binding -- check/validate when the line of code is executed.
  • Prioritisation/reservation --I think they mean if you do early binding and check the name spaces as the code is parsed there must be a process to validate a variable name when the variable is added dynamically later on and a different set of rules for what is valid as there may be several scopes involved at this point.

I am frankly surprised that only these quite technical reasons were given. Take the common case:-

onclick=' var mymark = "donethat";'

What namespace should "mymark" belong to? There doesnt seem to be any easy answer to that question. Especially given that the code snippet:-

window.forms.myform.mybutton.onClick = ' var mymark = "donethat";'

Should put the variable in the same name space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文