javascript 中对象的作用域与其上下文有什么区别?
用白话来说,范围和上下文有很多共同点。这就是为什么当我阅读对两者的引用时,我感到困惑,例如下面关于闭包的文章的引用:
范围是指变量和函数可以访问的位置,以及它在什么上下文中执行。 (@robertnyman)
据我所知告诉你,上下文只是对对象的引用。
有人可以解释一下context到底是什么吗,例如在jQuery语法中使用的$(selector, context)
。对象的范围与其上下文相同吗?
更新:
我发现这篇有趣的文章讨论了 JavaScript 中的范围和上下文。
http://www.digital-web.com/articles/scope_in_javascript/
In the vernacular, scope and context have a lot in common. Which is why I get confused when I read references to both, such as in the quote below from an article on closures:
Scope refers to where variables and functions are accessible, and in what context it is being executed. (@robertnyman)
As far as I can tell, context is just a reference to an object.
Can someone please explain what exactly is context, as used, for instance, in the jQuery syntax, $(selector, context)
. And is an object's scope the same at it's context?
Update:
I found this interesting article that talks about both scope and context in JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此 jQuery 示例中使用的“Context”不是 JavaScript 术语。它只是一个变量的名称。它与 jQuery 文档的其余部分一起记录: http://api.jquery.com/jQuery /#选择器上下文
"Context", as used in this jQuery example, is not a JavaScript term. It is just the name of a variable. It is documented with the rest of the jQuery documentation: http://api.jquery.com/jQuery/#selector-context
“上下文”可以意味着很多东西。在您引用的 jQuery 示例中,它是对应该应用选择器的 DOM 部分的引用,这与 JavaScript 上下文无关。
"context" can mean many things. In the jQuery example you quote it is a reference to a part of the DOM that the selector should be applied to, which has nothing to do with JavaScript contexts.