php isset() 等价于 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5436953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php isset() equivalent in javascript
提问by ptamzz
I'm looking for the javascript equivalent of the php function isset()
. I've tried the method described here at JavaScript isset() equivalentbut at firebug, error comes up saying
我正在寻找与 php 函数等效的 javascript isset()
。我已经尝试了在JavaScript isset() 等效处描述的方法,但在萤火虫中,出现错误说
data.del is undefined //Firebug warning/error
if(typeof data.del[0].node != 'undefined') { // codes in my js file
And in some cases
而且在某些情况下
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??
逻辑似乎有效,但我想知道为什么会出现错误?
Basically, I want to check whether data.del[0].node
or data.storyLine
isset or not??
基本上,我想检查是否data.del[0].node
或data.storyLine
isset与否?
采纳答案by zzzzBov
ECMAScript defines the hasOwnProperty
method for checking if an object has a property of a given name:
ECMAScript 定义了hasOwnProperty
检查对象是否具有给定名称的属性的方法:
var foo = {'bar':'bar'}
alert( foo.hasOwnProperty( 'bar' ) ); //true
alert( foo.hasOwnProperty( 'baz' ) ); //false
EDIT: This doesn't fully answer your question
编辑:这并不能完全回答你的问题
It's possible for a property to be set as undefined
可以将属性设置为 undefined
foo.bar = undefined;
alert( foo.hasOwnProperty( 'bar' ) ); //still true
The important question is: What do you need your truth table to be?
重要的问题是:你需要什么样的真值表?
In php:
在 php 中:
type | isset() | == true
------+---------+----------
null | false | false
false | true | false
true | true | true
"" | true | false
"a" | true | true
0 | true | false
1 | true | true
In JS:
在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
回答by Bobby D
isset() makes two checks: first if the variable is defined, and second if it is null.
isset() 进行两项检查:第一次是否定义了变量,第二次是否为空。
You will have to check for both the 'undefined' case and the null case, for example:
您必须同时检查“未定义”情况和空情况,例如:
if (typeof data !== 'undefined' && data !== null)
回答by Harmen
I think the best solution is to look in the source code of php.js:
我认为最好的解决方案是查看php.js的源代码:
function isset () {
// !No description available for isset. @php.js developers: Please update the function summary text file.
//
// version: 1103.1210
// discuss at: http://phpjs.org/functions/isset
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: FremyCompany
// + improved by: Onno Marsman
// + improved by: Rafa? Kukawski
// * example 1: isset( undefined, true);
// * returns 1: false
// * example 2: isset( 'Kevin van Zonneveld' );
// * returns 2: true
var a = arguments,
l = a.length,
i = 0,
undef;
if (l === 0) {
throw new Error('Empty isset');
}
while (i !== l) {
if (a[i] === undef || a[i] === null) {
return false;
}
i++;
}
return true;
}