typeof x =='undefined'或x == undefined? [英] typeof x == 'undefined' or x == undefined?

查看:118
本文介绍了typeof x =='undefined'或x == undefined?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过两种方式测试未定义的值:


函数等等(x)

{

if( x == undefined){x =''默认值''; }

}


也可以写成:


函数等等(x)

{

if(typeof x ==''undefined''){x =''默认值''; }

}


其中一个更正确比另一个?是否有一个实例,其中一个

可能会失败?


-Lost

You can test for an undefined value in two ways:

function blah(x)
{
if (x == undefined) { x = ''default value''; }
}

That could also have been written:

function blah(x)
{
if (typeof x == ''undefined'') { x = ''default value''; }
}

Is either one of these more "correct" than the other? Is there an instance where one
might fail over the other?

-Lost

推荐答案

-Lost在2007年1月28日下午3:59发表以下内容:
-Lost said the following on 1/28/2007 3:59 PM:

您可以通过两种方式测试未定义的值:


函数blah(x)

{

if(x == undefined){x =''默认值''; }

}


这也可以写成:
You can test for an undefined value in two ways:

function blah(x)
{
if (x == undefined) { x = ''default value''; }
}

That could also have been written:



不,因为它是测试两种不同的东西。

No, as it is testing two different things.


function blah(x)

{

if(typeof x ==' 'undefined''){x =''默认值''; }

}


其中一个更正确比另一个?是否有一个实例,其中一个

可能会失败?
function blah(x)
{
if (typeof x == ''undefined'') { x = ''default value''; }
}

Is either one of these more "correct" than the other? Is there an instance where one
might fail over the other?



这取决于你想要测试的内容。


如果(x == undefined)是测试值

if(typeof x = undefined)测试是否存在x


-

Randy
机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/

It depends - directly - on what you are trying to test for.

if(x == undefined) is testing the value
if (typeof x = undefined) is testing the existence of x

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


-Lost写道:
-Lost wrote:

您可以通过两种方式测试未定义的值:


函数blah(x)

{

if(x == undefined){x =''默认值''; } $ / $
}
You can test for an undefined value in two ways:

function blah(x)
{
if (x == undefined) { x = ''default value''; }
}



使用类型转换相等运算符有 - null - 等于 -

undefined - 。

Using the type-converting equality operator has - null - equalling -
undefined -.


也可以写成:


函数blah(x)

{

if(typeof x ==''undefined''){x =''默认值''; }

}


其中一个更正确比另一个?
That could also have been written:

function blah(x)
{
if (typeof x == ''undefined'') { x = ''default value''; }
}

Is either one of these more "correct" than the other?



正确测试取决于你想知道什么,以及你想知道它的

上下文。

The "correct" test is determined by what you want to know, and the
context in which you want to know it.


是否有实例一个可能会失败另一个?
Is there an instance where one might fail over the other?



他们是不同的测试,所以他们回答不同的问题。两者都会

失败如果在错误的情况下应用。


Richard。

They are different tests, so they answer different questions. Both would
"fail" if applied in the wrong situation.

Richard.


" Randy Webb" < Hi ************ @ aol.comwrote in message

news:xd ****************** ** @ telcove.net ...
"Randy Webb" <Hi************@aol.comwrote in message
news:xd********************@telcove.net...

-Lost在2007年1月28日下午3:59表示以下内容:
-Lost said the following on 1/28/2007 3:59 PM:

>您可以通过两种方式测试未定义的值:

函数blah(x)
{
if(x == undefined){ x =''默认值'';那也可以写成:
>You can test for an undefined value in two ways:

function blah(x)
{
if (x == undefined) { x = ''default value''; }
}

That could also have been written:



不,因为它正在测试两种不同的东西。


No, as it is testing two different things.


> function blah(x)
{
if(typeof x ==''undefined''){x ='''默认值'' ; }


其中一个更正确比另一个?是否存在一个
可能会失败的情况?
>function blah(x)
{
if (typeof x == ''undefined'') { x = ''default value''; }
}

Is either one of these more "correct" than the other? Is there an instance where one
might fail over the other?



这取决于你想要测试的内容。


如果(x == undefined)是测试值

if(typeof x = undefined)测试是否存在x


-

Randy
机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/



" Richard Cornford" < Ri ***** @ litotes.demon.co.ukwrote in message

新闻:ep ******************* @ news .demon.co.uk ...

"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...


-Lost写道:
-Lost wrote:

>你可以测试对于未定义的值有两种方式:

函数blah(x)
{
if(x == undefined){x =''默认值''; }
>You can test for an undefined value in two ways:

function blah(x)
{
if (x == undefined) { x = ''default value''; }
}



使用类型转换相等运算符有 - null - equaling - undefined - 。


Using the type-converting equality operator has - null - equalling - undefined -.


>也可以写成:

函数blah(x)
{
if(typeof x ==''undefined''){x = ''默认值''; }


其中一个更正确比另一个?
>That could also have been written:

function blah(x)
{
if (typeof x == ''undefined'') { x = ''default value''; }
}

Is either one of these more "correct" than the other?



正确测试取决于你想知道什么,以及你想知道它的上下文。


The "correct" test is determined by what you want to know, and the context in which you
want to know it.


>有没有一个人可能在另一个人身上失败的情况?
>Is there an instance where one might fail over the other?



他们是不同的测试,所以他们回答不同的问题。两者都会失败如果

在错误的情况下适用。


理查德。


They are different tests, so they answer different questions. Both would "fail" if
applied in the wrong situation.

Richard.



好​​的,我现在看到了。这个自学成才的速成课程让我积累了荒谬的假设。我肯定应该进一步测试它,也许那时我会有/ b $ b抓住/了解它。


谢谢,伙计们。


-Lost

OK, I see now. This self-taught crash course is causing me to amass ridiculous
assumptions. I definitely should have tested it further, perhaps then I would have
caught/understood that.

Thanks, guys.

-Lost


这篇关于typeof x =='undefined'或x == undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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