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

发布于 2024-12-01 12:51:04 字数 1044 浏览 2 评论 0原文

我有这个功能:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-12-08 12:51:04

您可以使用方括号表示法来引用名称存储在一个变量,所以您可能正在寻找这个:

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

}

此外,||运算符将返回第一个真值;如果将对象作为第一个参数传递会发生什么?这是事实,但您特别想要一个 string,因此虽然 || 运算符看起来很酷,但您可能会发现以下更合适:

if (typeof stringVar !== "string") {
    stringVar = "localize_en";
}

它看起来也让您感到困惑何时使用字符串来引用您的目标对象,何时不使用。

当你要做类似的事情时:

window[someVar]

someVar 需要 是一个字符串。

在 JavaScript 中可以通过引用传递对象,在编写上述所有内容以帮助您解决当前遇到的问题之后,更好的方法是在 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").
}

您应该将此函数传递给<强>字符串

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:

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

}

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:

if (typeof stringVar !== "string") {
    stringVar = "localize_en";
}

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:

window[someVar]

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:

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;
}

Edit:

From your comment, try this:

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").
}

You should pass this function a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文