[英]javascript singleton using arguments
I have the following pattern however I'd like to know if I am thinking about this the right way. 我有以下模式,但是我想知道我是否正在考虑正确的方法。 Do I need to assign the arguments to
this
? 我需要分配参数
this
? Is there anything you would do differently? 您有什么不同的做法吗?
var myFunction = (function()
{
function privateCheck(a,b)
{
console.log(a+b);
}
return
{
init: function(x,y)
{
privateCheck(x,y);
}
}
})();
myFunction.init(3,4);
myFunction.init(4,5);
Your anonymous, immediately-invoked function will always return undefined
. 立即调用的匿名函数将始终返回
undefined
。 Your return
statement trips over a common issue: 您的
return
声明涉及一个常见问题:
return { // <--- curly brace MUST be here
init: function(x,y)
{
privateCheck(x,y);
}
}
Other than that it should be OK, though there's not much context. 除此之外,尽管没有太多上下文,但应该可以。
edit the issue has to do with the often-weird rules about "semicolon insertion". 编辑问题与有关“分号插入”的常见规则有关。 In this particular case, like a handful of others, the language sees a newline after that
return
and assumes you just forgot the semicolon. 在这种特殊情况下,和少数其他情况一样,该语言在
return
后会看到换行符,并假定您只是忘记了分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.