【发布时间】:2011-07-23 03:47:48
【问题描述】:
我正在寻找 php 函数 isset() 的 javascript 等效项。我已经尝试过JavaScript isset() equivalent 此处描述的方法,但在萤火虫中,出现错误提示
data.del is undefined //Firebug warning/error
if(typeof data.del[0].node != 'undefined') { // codes in my js file
在某些情况下
data is null //Firebug warning/error
if(typeof data.storyLine != 'undefined') { // codes in my js file
逻辑似乎有效,但我想知道为什么会出现错误??
基本上,我想检查data.del[0].node 或data.storyLine isset 与否??
【问题讨论】:
-
或者你可以使用
hasOwnProperty -
请详细说明什么条件会为
isset()返回true的结果,具体为:NaN和undefined -
@zzzzBov,这只是我做一个 msql 查询,如果有数据,我会使用数组的 json_encode 返回它们。如果数据不存在,则什么都不返回。
-
然后使用
data.hasOwnProperty('foo'); -
@Jared Farrish,我清楚地记得看过lecture given by Douglas Crockford 提到了一些关于
in的问题,但我相信这是在for...in循环的上下文中。我相信in会在大多数情况下工作,但我更确定hasOwnProperty会按预期工作。
标签: javascript