这是用于创建将各种方法封装到不同对象中的名称空间的正确 JavaScript 吗?
var namespaced = {
A: function(){
function r(){
//do some stuff
return something;
}
var someProperty = 5;
function j(){
//do some more stuff
return something;
}
},
B: function(){
//can I call A and C?
A.r();
C.d();
},
C: function(){
function d() {
//do stuff we like
}
}
}
那我就可以做……
namespaced.A.j();
namespaced.C.d();
something = namespaced.A.someProperty;
对吧?
我也需要这样做吗?
var something = new namespaced.A()?
如果是的话 A() 有构造函数吗?我在这里真的很困惑:{
我正在尝试封装我的 javascript,以便它易于维护
var namespaced = {
A: function(){
function r(){
//do some stuff
return something;
}
var someProperty = 5;
function j(){
//do some more stuff
return something;
}
},
B: function(){
//can I call A and C?
A.r();
C.d();
},
C: function(){
function d() {
//do stuff we like
}
}
}
Then I could do...
namespaced.A.j();
namespaced.C.d();
something = namespaced.A.someProperty;
right?
Would I need to do this too?
var something = new namespaced.A()?
If so does A() have a constructor? I'm really confused here :{
I'm trying to encapsulate my javascript so it's easy to maintain
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能。函数
j
和someProperty
仅对于A
是本地的,不会传播到外部。如果您想从外部访问它们,则必须使用this
将它们设为函数的属性:但您仍然需要调用
var a = new namespaced.A()
以便访问这些功能。如果你想直接调用
namespaced.Aj()
,你必须将A
声明为对象,而不是函数:所以这取决于你最终想要实现的目标。 ..为了更好地了解这些方法,我推荐 JavaScript 模式。
No you couldn't. The function
j
andsomeProperty
are only local toA
and are not propagated to the outside. If you want to access them from the outside, you have to make them a property of the function, usingthis
:But you would still need to call
var a = new namespaced.A()
in order to access the functions.If you want to call
namespaced.A.j()
directly, you would have to declareA
as object, not as function:So it depends on what you want to achieve eventually... to get a better insight into these methods, I recommend JavaScript Patterns.
这是您需要了解的有关 JavaScript 的内容:
您正在创建(并分配给
obj
)一个具有名为A
、B
属性的对象> 和C
分别映射到值a
、b
和c
。这些值很可能是函数,因此当您创建一个具有名为“A”的属性的对象时,它是一个函数。您可以使用 obj.A 引用它并使用 obj.A() 调用。
obj.A()
时,函数A
体内的关键字this
将引用obj
。您可以使用它为obj
分配新属性:因此,在
namespaced.Aj()
内,this
关键字将指向namespace .A
(它是最后一个点左侧的内容)。您可以将函数应用于对象,如下所示:
func.apply(obj)
或如下所示:func.call(obj)
。在这种情况下,this
关键字将改为引用obj
。这与您的情况无关,但如果func
接受参数(比如说param1
和param2
),您可以像这样应用该函数:func.apply(obj, [val1, val2])
或类似:func.call(obj, val1, val2)
。函数内声明的所有变量仅存在于该函数内。它们在外面是看不见的。当你编写
function doStuff(){}
时,它(我在这里进行了简化)就像你编写var doStuff = function(){};
一样好,所以嵌套函数live 且只能在周围函数内部使用;也就是说,除非您将它们分配给可从外部访问的内容。当您调用诸如
new Cons()
之类的内容时,会创建一个新的空对象,然后在该对象上应用Cons()
。换句话说,它与或者如果您愿意的话:
所以你可以这样:
With all the preceding in mind, a way to write namespaced code is like this:
我希望这能有所帮助,而不是造成困惑。
This is what you need to understand about JavaScript:
you are creating (and assigning to
obj
) an object with properties calledA
,B
andC
mapping to valuesa
,b
andc
respectively. These values may very well be functions, so when you haveyou are creating an object with a property called "A" which is a function. You can refer to it with
obj.A
and call withobj.A()
.obj.A()
, the keywordthis
inside the body of functionA
will refer toobj
. You can use it to assign new properties toobj
:So, inside
namespaced.A.j()
thethis
keyword will point tonamespace.A
(it's what is to the left of the last dot).You can apply a function to an object like so:
func.apply(obj)
or like so:func.call(obj)
. In this case, thethis
keyword will refer toobj
instead. This isn't relevant to your case, but iffunc
takes parameters (let's sayparam1
andparam2
), you can apply the function like so:func.apply(obj, [val1, val2])
or like so:func.call(obj, val1, val2)
.All variables declared inside a function live only inside that function. They are not visible outside. And when you write
function doStuff(){}
it's (I'm simplifying here) as good as if you wrotevar doStuff = function(){};
So nested functions live and can be used only inside the surrounding function; that is, unless you assign them to something accessible from outside.When you call something like
new Cons()
what happens is the creation of a new empty object followed by the application ofCons()
on that object. In other words, it's the same asor if you prefer:
So you can have this:
With all the preceding in mind, a way to write namespaced code is like this:
I hope this helps more than it confuses.