JavaScript 将作用域传递给另一个函数
是否有可能以某种方式将一个函数的作用域传递给另一个函数?
例如,
function a(){
var x = 5;
var obj = {..};
b(<my-scope>);
}
function b(){
//access x or obj....
}
我宁愿直接访问变量,即不使用 this.a
或 this.obj
之类的东西,而只使用 x
或直接obj
。
Is it possible to somehow pass the scope of a function to another?
For example,
function a(){
var x = 5;
var obj = {..};
b(<my-scope>);
}
function b(){
//access x or obj....
}
I would rather access the variables directly, i.e., not using anything like this.a
or this.obj
, but just use x
or obj
directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
真正访问函数
a
的私有作用域的唯一方法是在a
内部声明b
,这样它就形成了一个允许隐式调用的闭包。访问a
的变量。这里有一些供您选择。
直接访问
在
a
内部声明b
。如果您不想将
b
放在a
内,那么您可以将它们都放在更大的容器范围内:这些是唯一的方法您将能够直接在
b
中使用a
的变量,而无需一些额外的代码来移动事物。如果您满足于一点“帮助”和/或间接,这里还有一些想法。间接访问
您可以只将变量作为参数传递,但除了对象的属性之外,没有写访问权限:
作为此方法的变体,您可以通过将更新的值传递回
a
来获得写入权限,如下所示:您可以向
b
传递一个带有 getter 和 setter 的对象,这些 getter 和 setter 可以访问a
的私有变量:getter 和 setter 可以是公共的,分配给
a
的this
对象,但这样只有在a 中显式给出时才能访问它们
。您可以将变量放入一个对象中并传递该对象:
作为一种变体,您可以在调用时构造对象:
The only way to truly get access to function
a
's private scope is to declareb
inside ofa
so it forms a closure that allows implicit access toa
's variables.Here are some options for you.
Direct Access
Declare
b
inside ofa
.If you don't want
b
inside ofa
, then you could have them both inside a larger container scope:These are the only ways you're going to be able to use
a
's variables directly inb
without some extra code to move things around. If you are content with a little bit of "help" and/or indirection, here are a few more ideas.Indirect Access
You can just pass the variables as parameters, but won't have write access except to properties of objects:
As a variation on this you could get write access by passing updated values back to
a
like so:You could pass
b
an object with getters and setters that can accessa
's private variables:The getters and setters could be public, assigned to the
this
object ofa
, but this way they are only accessible if explicitly given out from withina
.And you could put your variables in an object and pass the object around:
As a variation you can construct the object at call time instead:
作用域是由函数创建的,作用域与函数保持一致,因此最接近您要求的是将函数从
a()
传递到b()< /code>,并且该函数将继续访问
a()
中的作用域变量。虽然
b()
无法直接访问函数传递的变量,但如果函数传递返回,则可以访问传递引用的数据类型(例如对象) /em> 那个对象。Scope is created by functions, and a scope stays with a function, so the closest thing to what you're asking will be to pass a function out of
a()
tob()
, and that function will continue to have access to the scoped variables froma()
.While
b()
doesn't have direct access to the variables that the function passed does, data types where a reference is passed, like an Object, can be accessed if the function passed returns that object.使用
bind
怎么样what about using
bind
不。
您正在访问本地范围对象。
[[上下文]]
。您无法公开访问它。
现在,由于它是 node.js,您应该能够编写一个 C++ 插件,让您可以访问
[[Context]]
对象。我强烈建议不要这样做,因为它给 JavaScript 语言带来了专有的扩展。No.
You're accessing the local scope object. The
[[Context]]
.You cannot publicly access it.
Now since it's node.js you should be able to write a C++ plugin that gives you access to the
[[Context]]
object. I highly recommend against this as it brings proprietary extensions to the JavaScript language.你不能“通过范围”……据我所知,情况并非如此。
您可以使用
apply
或call
传递函数引用的对象,并将当前对象 (this
) 作为第一个参数发送仅调用函数:函数访问特定范围的唯一方法是在该范围内声明。
有点棘手。
这会导致类似的结果:
但这有点违背了目的。
You can't "pass the scope"... not that I know of.
You can pass the object that the function is referring to by using
apply
orcall
and send the current object (this
) as the first parameter instead of just calling the function:The only way for a function to access a certain scope is to be declared in that scope.
Kind'a tricky.
That would lead to something like :
But that would kind of defeat the purpose.
正如其他人所说,你不能这样传递范围。但是,您可以使用自执行匿名函数正确地确定变量的范围(如果您比较迂腐,则可以立即执行):
As others have said, you cannot pass scope like that. You can however scope variables properly using self executing anonymous functions (or immediately executing if you're pedantic):
我认为您可以做的最简单的事情就是将变量从一个作用域传递到该作用域之外的函数。如果您通过引用传递(如对象),则 b 可以“访问”它(请参阅下面的 obj.someprop):
I think the simplest thing you can do is pass variables from one scope to a function outside that scope. If you pass by reference (like Objects), b has 'access' to it (see obj.someprop in the following):
你实际上只能通过 eval 来做到这一点。下面将给出函数 b 函数 a 的作用域
You can really only do this with eval. The following will give function b function a's scope
您是否尝试过这样的事情:
如果您可以将函数 B 作为方法函数 A,那么请执行以下操作:
Have you tried something like this:
If you can stand function B as a method function A then do this: