php isset()在javascript中等效 [英] php isset() equivalent in javascript
问题描述
我正在寻找与PHP函数 isset()
相当的javascript。我在 JavaScript isset()等效中尝试了此处描述的方法,但在firebug中,出现了错误说法
I'm looking for the javascript equivalent of the php function isset()
. I've tried the method described here at JavaScript isset() equivalent but at firebug, error comes up saying
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
逻辑似乎有效,但我想知道为什么会出现错误呢? ?
The logic seems to work but I'm wondering why is there an error then??
基本上,我想检查 data.del [0] .node
或 data.storyLine
isset or not ??
Basically, I want to check whether data.del[0].node
or data.storyLine
isset or not??
推荐答案
ECMAScript定义 hasOwnProperty
检查对象是否具有给定名称的属性的方法:
ECMAScript defines the hasOwnProperty
method for checking if an object has a property of a given name:
var foo = {'bar':'bar'}
alert( foo.hasOwnProperty( 'bar' ) ); //true
alert( foo.hasOwnProperty( 'baz' ) ); //false
编辑:这不能完全回答你的问题
This doesn't fully answer your question
可以将房产设置为 undefined
foo.bar = undefined;
alert( foo.hasOwnProperty( 'bar' ) ); //still true
重要的问题是:你需要什么样的真值表?
The important question is: What do you need your truth table to be?
在php中:
type | isset() | == true
------+---------+----------
null | false | false
false | true | false
true | true | true
"" | true | false
"a" | true | true
0 | true | false
1 | true | true
在JS中:
type | isset() | truthy
----------+---------+--------
NaN | ? | false
undefined | ? | false
null | false | false
true | true | true
false | true | false
"" | true | false
"a" | true | true
0 | true | false
1 | true | true
这篇关于php isset()在javascript中等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!