如何检查 JavaScript 中定义的参数和传递的参数?
我有这个功能:
function(stringsVar) {
var stringRes = stringsVar || localize_en;
if('window.'+stringsVar === undefined) {
stringRes = localize_en;
}
...
}
但不起作用。实际上就像这样:
function(stringsVar) {
var stringRes = stringsVar || localize_en;
}
该函数可以带参数,也可以不带参数,上面的代码正在正确检查它。该函数的参数将是一个变量。我想将这种能力添加到我的函数中。它将检查该变量是否已定义。如果系统中没有定义变量 localize_en,它将被分配为默认值。
我该如何更正我的代码。我的代码的第二部分是该功能: 即 stringsVar 是 localize_ar 并且它不是已定义的变量(我使用 var 关键字定义这种变量)
if(window.localize_ar === undefined){
alert('yes');}
else {
alert('no');
}
我会将该功能添加为参数。
有什么想法吗?
PS: localize_en 和类似的变量是对象。
编辑:我正在研究 JQuery 本地化插件 => 源代码。 我称它为
$('html').localize('localize_' + tr);
但是它不能将其理解为一个对象,它的工作原理就像我一样:
$('html').localize(localize_tr);
它将它更改为字符串也许问题就在那里?
I have that function:
function(stringsVar) {
var stringRes = stringsVar || localize_en;
if('window.'+stringsVar === undefined) {
stringRes = localize_en;
}
...
}
and doesn't work. That was like that actually:
function(stringsVar) {
var stringRes = stringsVar || localize_en;
}
that function can take a parameter or not and the code above is checking it correctly. Parameter of that function will be a variable. I want to add that ability to my function. It will check whether that variable is defined or not. If not there is a defined variable at system, localize_en, it will be assigned as default.
How can I correct my code. The second part of my code will be that functionality:
i.e stringsVar is localize_ar and it is not a defined variable (I define that kind of variables with var keyword)
if(window.localize_ar === undefined){
alert('yes');}
else {
alert('no');
}
I will add that functionality as parametric.
Any ideas?
PS: localize_en and something like that variables are object.
EDIT: I am working on JQuery localizer plugin => source code.
I call it as
$('html').localize('localize_' + tr);
However it can not understand it as an object, it works as if I do:
$('html').localize(localize_tr);
It changes it into a string maybe the problem lays on there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用方括号表示法来引用名称存储在一个变量,所以您可能正在寻找这个:
此外,
||
运算符将返回第一个真值;如果将对象作为第一个参数传递会发生什么?这是事实,但您特别想要一个 string,因此虽然||
运算符看起来很酷,但您可能会发现以下更合适:它看起来也让您感到困惑何时使用字符串来引用您的目标对象,何时不使用。
当你要做类似的事情时:
someVar
需要 是一个字符串。在 JavaScript 中可以通过引用传递对象,在编写上述所有内容以帮助您解决当前遇到的问题之后,更好的方法是在 JavaScript 中通过引用传递对象首先放置并完全避免问题,而不是传递存储对象的变量的名称:
编辑:
根据您的评论,尝试这样做:
您应该将此函数传递给<强>字符串。
You can use the square bracket notation to refer to object members whose name is stored in a variable, so you're probably looking for this:
Furthermore, the
||
operator will return the first truthy; what happens if an object is passed as the first parameter? That's truthy, but you specifically want a string, so whilst the||
operator looks cool, you might find the following more appropiate:It also looks like you're getting confused when to use a string to refer to the object your targeting, and when not to.
When you going to be doing something like:
someVar
needs to be a string.It is possible to pass an object by reference in JavaScript, and after writing all the above to help you fix the problem you've currently got, a better approach will be to pass the object by reference in the first place and avoid the problem completely, rather than passing the name of the variable storing the object:
Edit:
From your comment, try this:
You should pass this function a string.