javascript/jQuery 中有类似 php isset 的东西吗?
javascript/jQuery 中有没有东西可以检查变量是否设置/可用?在 php 中,我们使用 isset($variable) 来检查类似的内容。
谢谢。
Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable)
to check something like this.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
尝试这个表达式:
如果变量已定义且不为 null,则该表达式为 true,这相当于 PHP 的 isset 的工作方式。
你可以这样使用它:
Try this expression:
This will be true if the variable is defined and not null, which is the equivalent of how PHP's isset works.
You can use it like this:
PHP JS 上的 JavaScript isset()
JavaScript isset() on PHP JS
typeof 会达到我认为的目的
typeof will serve the purpose I think
如果您想检查属性是否存在: hasOwnProperty 是 由于大多数
对象都是其他对象的属性(最终导致
window
对象),因此这可以很好地检查值是否已声明。If you want to check if a property exists: hasOwnProperty is the way to go
And since most objects are properties of some other object (eventually leading to the
window
object) this can work well for checking if values have been declared.每个答案的某些部分都有效。我将它们全部编译成一个函数“isset”,就像问题所问的那样,并且像在 PHP 中一样工作。
以下是如何使用它的用法示例:
这取决于您需要它的情况,但让我分解一下每个部分的作用:
typeof(variable) !== "undefined"
检查是否变量完全被定义variable !== null
检查变量是否为 null(有些人显式设置 null 并且不认为设置为 null 就是正确的,在这种情况下,删除这部分)variable !== ''
检查变量是否设置为空字符串,如果空字符串被视为为您的用例设置,您可以删除它希望这对某人有帮助:)
Some parts of each of these answers work. I compiled them all down into a function "isset" just like the question was asking and works like it does in PHP.
Here is a usage example of how to use it:
It depends on the situation you need it for but let me break down what each part does:
typeof(variable) !== "undefined"
checks if the variable is defined at allvariable !== null
checks if the variable is null (some people explicitly set null and don't think if it is set to null that that is correct, in that case, remove this part)variable !== ''
checks if the variable is set to an empty string, you can remove this if an empty string counts as set for your use caseHope this helps someone :)
不自然,不...但是,谷歌搜索给出了这个: http://phpjs.org/功能/设置:454
Not naturally, no... However, a googling of the thing gave this: http://phpjs.org/functions/isset:454
http://phpjs.org/functions/isset:454
phpjs 项目是值得信赖的来源。那里有很多 js 等效的 php 函数。我已经使用了很长时间了,到目前为止没有发现任何问题。
http://phpjs.org/functions/isset:454
phpjs project is a trusted source. Lots of js equivalent php functions available there. I have been using since a long time and found no issues so far.
问题在于将未定义的变量传递给函数会导致错误。
这意味着您必须在将其作为参数传递之前运行 typeof 。
我发现执行此操作的最干净的方法如下所示:
用法:
现在代码更加紧凑和可读。
如果您尝试从非实例化变量调用变量,这仍然会给出错误:
因此在尝试运行此变量之前,您需要确保定义了该对象:
The problem is that passing an undefined variable to a function causes an error.
This means you have to run typeof before passing it as an argument.
The cleanest way I found to do this is like so:
Usage:
Now the code is much more compact and readable.
This will still give an error if you try to call a variable from a non instantiated variable like:
thus before trying to run this you need to make sure the object is defined:
这里 :)
Here :)
除了 @emil-vikström 的答案之外,检查
variable!=null
对于variable!==null
以及对于变量!==未定义
(或typeof(变量)!=“未定义”
)。in addition to @emil-vikström's answer, checking for
variable!=null
would be true forvariable!==null
as well as forvariable!==undefined
(ortypeof(variable)!="undefined"
).你可以:
You can just: