Javascript:将一个未定义的变量传递给一个函数 [英] Javascript: passing a not defined variable to a function

查看:84
本文介绍了Javascript:将一个未定义的变量传递给一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些调用AJAX的地方,我返回一个JSON对象作为响应。这个JSON对象总是具有相同的结构。但是,有时PHP脚本失败的地方,JSON结构的一些对象没有被定义,甚至不存在。



这也发生在一些全局在一个页面中存在的变量,我用在一些JS文件中,但随后到另一个页面,我使用这些JS文件,甚至没有写入变量。



所以,我想创建一个函数 is_defined(),我可以询问是否定义了一个变量。该函数如下所示:

 函数is_defined(ob,can_empty){
if(can_empty === undefined || can_empty =='')can_empty = false

return ob!== undefined&& ob!== null&& (ob!==''|| can_empty)
}

所以,如果变量传递的是 NULL undefined ,或者它是空的(并且它不能为空),我返回 false



当变量没有写入时,我的问题就出现了,因为我不能将它传递给函数,因为 Uncaught ReferenceError:obj未定义错误被引发。

所以我的问题是如果有没有什么办法可以创建一个函数来检查一个变量是否存在并且是定义 ,因为我想将所有这些检查集中到一个函数中,而不是编写 obj!== undefined | | obj!== null 每隔一段时间都在我的代码中。



我认为用双运算符(<$ c























<编辑:
作为一个声音,这里的问题是能够在函数内部对未声明和未定义的变量进行检查,所以我可以将一个变量传递给该函数在调用变量之前不必检查它。



谢谢大家的时间和答案!



亲切的问候!

解决方案


[... ]有没有什么办法可以创建一个函数来检查一个变量是否存在并被定义[...]

不是没有。当你将一个变量传递给一个函数时,它总是会尝试解析该变量。如果没有声明,它会抛出一个引用错误。



ECMAScript规范中的相关部分



您必须检查变量是否通过 typeof 之前,您将它传递给一个函数。


I have some calls to AJAX where I return a JSON object as a response. This JSON object always has the same structure. However, there are sometimes where if the PHP scripts fails, some of the objects of the JSON structure are not defined or even inexistent.

This also happens with some "global" variables that are present in a page and I use in some JS files, but then go to another page where I use those JS files and the variable is not even written.

So, I thought to create a function is_defined() to which I could ask if a variable is defined or not. The function is as the following:

function is_defined(ob, can_empty){
    if(can_empty === undefined || can_empty == '') can_empty = false

    return ob !== undefined && ob !== null && (ob !== '' || can_empty)
}

So, if the variable passed is NULL or undefined, or it's empty (and it cannot be empty), I return false.

My problem comes when the variable is not even written, as I cannot pass it to the function because an Uncaught ReferenceError: obj is not defined error is raised.

So my question is if is there any way to create a function to check if a variable exists and is defined, as I'd like to centralize all these checks into a function and not writing obj !== undefined || obj !== null every now and then on my code.

I thought that with a double Not operator (!!!obj) would cast to false, but I face the same problem if the variable is not written.

EDIT: Just as an aclaration, the issue here is to be able to do the checks for undeclared and undefined variables inside a function, so I can pass a variable to the function without having to check it before calling it if the variable is declared or not.

Thank you all for your time and answers!

Kind regards!

解决方案

[...] is there any way to create a function to check if a variable exists and is defined [...]

No there is not. When you pass a variable to a function, it will always try to resolve that variable. If it is not declared it will throw a reference error.

Relevant section from the ECMAScript specification.

You have to check whether the variable is declared via typeof before you pass it to a function.

这篇关于Javascript:将一个未定义的变量传递给一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆