测试以查看变量是否存在... [英] Testing to see if a variable exists...

查看:52
本文介绍了测试以查看变量是否存在...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Folks,


任何人都知道如何在javascript中测试变量的存在?


我正在寻找相当于PHP的IsSet()。我遇到了麻烦

找到它。


thx


-

im

美国爱国者法案是美国历史上最不爱国的行为。

Hey Folks,

Anyone know how to test for the existance of a variable in javascript?

I''m looking for the equivalent of IsSet() from PHP. I''m having trouble
finding it.

thx

--
i.m.
The USA Patriot Act is the most unpatriotic act in American history.

推荐答案

Ivan Marsh写道:
Ivan Marsh wrote:
任何人都知道如何在javascript中测试变量的存在?
Anyone know how to test for the existance of a variable in javascript?




检查''typeof ''运营商:


< script type =" text / javascript">

alert(typeof foo);

if(typeof foo ==" undefined"){

//做东西......

}

< / script>


学习这个属性时,最好在不同的用户代理中迭代许多
js / html类型,以检查返回的内容价值。

你有时可能会感到惊讶(尤其是js中的null和Mozilla / IE中的HTML

元素)。

HTH

是的。



Check the ''typeof'' operator:

<script type="text/javascript">
alert( typeof foo );
if(typeof foo == "undefined") {
// do stuff...
}
</script>

It''s a good idea, when learning this property, to iterate over many
js/html types, in different user agents, to check the returned value.
You might be surprised sometimes (especially for "null" in js, and HTML
elements in Mozilla/IE).
HTH
Yep.




Ivan Marsh写道:


Ivan Marsh wrote:
任何人都知道如何在javascript中测试变量的存在?

我正在寻找PHP的等效IsSet()。我很难找到它。
Anyone know how to test for the existance of a variable in javascript?

I''m looking for the equivalent of IsSet() from PHP. I''m having trouble
finding it.




与PHP中的isset不完全相同但JavaScript方式通常是

if(typeof varname!=''undefined''){

//使用varName

}

然而这种方法并不是允许您区分

变量是否尚未声明或已声明但尚未初始化,

如果您声明

var varname ;

然后

typeof varname

仍然是'undefined''。但这通常不会打扰你,因为你只需要b $ b想要检查变量中是否存储了有用的东西。


-


Martin Honnen
http://JavaScript.FAQTs.com /



Not quite the same as isset in PHP but the JavaScript way is usually
if (typeof varname != ''undefined'') {
// use varName
}
However that approach doesn''t allow you to distinguish whether a
variable has not been declared or has been declared but not intialized,
that is if you declare
var varname;
then
typeof varname
is still ''undefined''. But that usually doesn''t bother you as you only
want to check whether something useful is stored in the variable.

--

Martin Honnen
http://JavaScript.FAQTs.com/


" Ivan Marsh"写于2003年11月11日:
"Ivan Marsh" wrote on 11/11/2003:
嘿民众,

任何人都知道如何测试
javascript中变量的存在?<我正在寻找PHP中等效的IsSet()。我找不到
麻烦。
Hey Folks,

Anyone know how to test for the existance of a variable in javascript?
I''m looking for the equivalent of IsSet() from PHP. I''m having trouble finding it.




使用此功能:


函数isSet(变量)

{

return(typeof(variable)!=''undefined'');

}


如果定义了变量但尚未分配变量,它将起作用。对于

示例:


var a;

var b =''used'';

isSet(a); // false

isSet(b); // true


如果你想测试那些根本没有声明的变量,你需要直接测试,否则会发生错误。继续上面的




if(typeof(c)==''undefined'')

{

//这将在此示例中执行

// c未定义或尚未分配给...

}

其他

{

//已定义c ...

}


Mike


-

Michael Winter

M.Winter @ [no-spam] blueyonder.co.uk(删除[无垃圾邮件]回复)



Use this function:

function isSet( variable )
{
return( typeof( variable ) != ''undefined'' );
}

It will work if a variable is defined, but not assigned to yet. For
example:

var a;
var b = ''used'';

isSet( a ); // false
isSet( b ); // true

If you want to test for variables that have not been declared at all,
you have to test directly, or an error will occur. Continuing from
above:

if( typeof( c ) == ''undefined'' )
{
// this will execute in this example
// c is not defined or has not been assigned to...
}
else
{
// c has been defined...
}

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


这篇关于测试以查看变量是否存在...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆