javascript/jQuery 中是否有类似 php 的isset 之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231789/
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
is there something like isset of php in javascript/jQuery?
提问by gautamlakum
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.
javascript/jQuery 中有什么东西可以检查变量是否设置/可用?在 php 中,我们isset($variable)用来检查这样的事情。
thanks.
谢谢。
回答by Emil Vikstr?m
Try this expression:
试试这个表达式:
typeof(variable) != "undefined" && variable !== null
This will be true if the variable is defined and not null, which is the equivalent of how PHP's isset works.
如果变量已定义且不为空,则为 true,这与 PHP 的 isset 的工作方式等效。
You can use it like this:
你可以这样使用它:
if(typeof(variable) != "undefined" && variable !== null) {
bla();
}
回答by Oleg
function isset () {
// 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;
}
回答by zzzzBov
If you want to check if a property exists: hasOwnPropertyis the way to go
如果你想检查一个属性是否存在:hasOwnProperty是要走的路
And since most objects are properties of some other object (eventually leading to the windowobject) this can work well for checking if values have been declared.
并且由于大多数对象是某个其他对象的属性(最终导致该window对象),因此可以很好地检查值是否已声明。
回答by DanneManne
typeof will serve the purpose I think
typeof 将达到我认为的目的
if(typeof foo != "undefined"){}
回答by stuyam
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.
每个答案的某些部分都有效。我将它们全部编译成一个函数“isset”,就像问题所问的那样,并且像在 PHP 中一样工作。
// isset helper function var isset = function(variable){ return typeof(variable) !== "undefined" && variable !== null && variable !== ''; }
// isset helper function var isset = function(variable){ return typeof(variable) !== "undefined" && variable !== null && variable !== ''; }
Here is a usage example of how to use it:
以下是如何使用它的用法示例:
var example = 'this is an example';
if(isset(example)){
console.log('the example variable has a value set');
}
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 !== nullchecks 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 case
typeof(variable) !== "undefined"检查变量是否已定义variable !== null检查变量是否为空(有些人明确设置为空,不认为它是否设置为空是正确的,在这种情况下,删除这部分)variable !== ''检查变量是否设置为空字符串,如果空字符串计为您的用例设置,您可以删除它
Hope this helps someone :)
希望这对某人有所帮助:)
回答by Ioannis Karadimas
Not naturally, no... However, a googling of the thing gave this: http://phpjs.org/functions/isset:454
不自然,不......但是,谷歌搜索给出了这个:http: //phpjs.org/functions/isset: 454
回答by Sandeepan Nath
http://phpjs.org/functions/isset:454
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.
phpjs 项目是一个值得信赖的来源。那里有很多 js 等效的 php 函数。我已经使用了很长时间,到目前为止没有发现任何问题。
回答by Dieter Gribnitz
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.
这意味着您必须在将其作为参数传递之前运行 typeof。
The cleanest way I found to do this is like so:
我发现这样做的最干净的方法是这样的:
function isset(v){
if(v === 'undefined'){
return false;
}
return true;
}
Usage:
用法:
if(isset(typeof(varname))){
alert('is set');
} else {
alert('not set');
}
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:
如果您尝试从非实例化变量中调用变量,这仍然会产生错误,例如:
isset(typeof(undefVar.subkey))
thus before trying to run this you need to make sure the object is defined:
因此,在尝试运行之前,您需要确保已定义对象:
undefVar = isset(typeof(undefVar))?undefVar:{};
回答by Mr Bill
Here :)
这里 :)
function isSet(iVal){
return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0;
} // Returns 1 if set, 0 false
回答by user2619604
in addition to @emil-vikstr?m's answer, checking for variable!=nullwould be true for variable!==nullas well as for variable!==undefined(or typeof(variable)!="undefined").
除了@emil-vikstr?m的答案之外,检查 for以及 for (or )variable!=null都是真的。variable!==nullvariable!==undefinedtypeof(variable)!="undefined"

