如何检查JavaScript传递的定义和参数?

如何检查JavaScript传递的定义和参数?,javascript,jquery,window,undefined,jquery-localizer,Javascript,Jquery,Window,Undefined,Jquery Localizer,我有这个功能: function(stringsVar) { var stringRes = stringsVar || localize_en; if('window.'+stringsVar === undefined) { stringRes = localize_en; } ... } 而且不起作用。实际上是这样的: function(stringsVar) { var stringRes = stringsVar || localize_en; } 该函数可以接受参

我有这个功能:

function(stringsVar) {
var stringRes = stringsVar || localize_en;
if('window.'+stringsVar === undefined) {
    stringRes = localize_en;
}
...
}
而且不起作用。实际上是这样的:

function(stringsVar) {
    var stringRes = stringsVar || localize_en;
}
该函数可以接受参数,也可以不接受参数,上面的代码正在正确地检查它。该函数的参数将是一个变量。我想把这种能力添加到我的功能中。它将检查是否定义了该变量。如果系统中没有定义的变量localize_en,它将被指定为默认值

如何更正我的代码。我的代码的第二部分将是该功能: i、 e stringsVar是localize_ar,它不是一个已定义的变量(我用var关键字定义这类变量)

我将添加该功能作为参数

有什么想法吗

PS:localize_en和类似于变量是对象的东西

编辑:我正在开发JQuery定位器插件=>。 我称之为

$('html').localize('localize_' + tr);
然而,它无法将其理解为一个对象,它的工作原理就像我所做的那样:

$('html').localize(localize_tr);
它将其更改为字符串可能是问题所在?

您可以使用来引用名称存储在变量中的对象成员,因此您可能正在查找以下内容:

if (window[stringsVar] === undefined) {

}
此外,
|
操作符将返回第一个truthy;如果对象作为第一个参数传递,会发生什么情况?这是事实,但您特别需要一个字符串,因此尽管
|
操作符看起来很酷,但您可能会发现以下内容更合适:

if (typeof stringVar !== "string") {
    stringVar = "localize_en";
}
您似乎还弄不清楚何时使用字符串来引用目标对象,何时不使用字符串

当你要做一些事情时,比如:

window[someVar]
someVar
需要成为字符串

在JavaScript中通过引用传递对象是可能的,在编写上述所有内容以帮助您解决当前遇到的问题之后,更好的方法是首先通过引用传递对象,并完全避免问题,而不是传递存储对象的变量的名称:

function(obj) {
    if (typeof obj !== "object") { 
        obj = localize_en; // here we're wanting the object itself, rather than the name of the object, so we're not using a string.
    };

    // Now use `obj`. It'll be either the object the user passed, or the default (localize_en).

    // You can even store this in a global variable if you want to:
    window.selected_obj = obj;
}
编辑: 从您的评论中,尝试以下内容:

function (stringsVar) {
    if (typeof stringsVar !== "string" || typeof window[stringsVar] !== "object") {
        stringsVar = "localize_en"; // Set the default of the argument, if either none is provided, or it isn't a string, or it doesn't point to a valid object
    }

    var stringRes = window[stringsVar];

    // Now do *whatever* you want with stringRes. It will either be the *valid* localization type the parameter specified, or the default ("localize_en").
}

您应该向这个函数传递一个字符串

本地化\u en,本地化\u tr是我的独立文件,在这些文件中,我声明它们像var localize\u tr=。。。我正在获取浏览器语言,即ar,我将检查是否有名为localize_ar的变量,如果没有,则我将使用localize_en。另一方面,如果我不想检查浏览器语言,我不会将参数传递给该函数,它的行为类似于本地化。我想改进jquery定位器插件。谢谢,效果很好。只是需要了解一下,传递对象而不是将其名称作为字符串传递的缺点是什么?我的意思是我可以检查它是否是一个对象,是否继续,或者分支到您的代码?@kamaci:您的问题使我们所处的上下文松散;这里的问题不在于是否传递对象或字符串,问题在于我们是否应该传递一个可能未定义的字符串或对变量(对象或非对象)的引用。正因为这个原因,我喜欢在这种情况下传递字符串;因此,在每次调用函数时传递变量之前,不必检查变量是否已定义。
function (stringsVar) {
    if (typeof stringsVar !== "string" || typeof window[stringsVar] !== "object") {
        stringsVar = "localize_en"; // Set the default of the argument, if either none is provided, or it isn't a string, or it doesn't point to a valid object
    }

    var stringRes = window[stringsVar];

    // Now do *whatever* you want with stringRes. It will either be the *valid* localization type the parameter specified, or the default ("localize_en").
}