Javascript Javascript 每日测试 - 外一篇 0

这是期前讨论的,由于比较琐碎,所以整理在一起。

大家认为如下的函数会返回什么?

function test() {
    return
    {
        status: true
    };
}

答案是返回 undefined,因为 return 后面有回车(被认为是条完整的语句),可以认为等效下面的代码

function test() {
    return;
    {
        status: true
    };
}

清羽 同学从 ECMA 上找到了相应的解释

ReturnStatement:
    return [no LineTerminator here] Expressionopt ;    ——from Ecma-262

-- Split --

var Obj = {};
alert(Obj.abc == undefined); //true 

undefined = 'hello, world';
alert(undefined == 'hello, world'); //true

Obj.abc = 'hello, world';
alert(Obj.abc == undefined);

上述的代码最后 alert 的结果为 true 还是 false ?

在 Mozilla 相关的 Javascript 文档中,对于 undefined 的描述中可以得知

undefined is a property of the global object, i.e. it is a variable in global scope. 

The initial value of undefined is the primitive value undefined.

所以,“undefined” 的值是可以被覆盖的。所以,上述的答案为 true 。另,相对于“undefined”,null 则为关键字(来源

null, a special keyword denoting a null value; null is also a primitive value. 
Because JavaScript is case-sensitive, null is not the same as Null, NULL, 
or any other variant

“同样的还有NaN, 可以试一下:”from 小马(相关解释)。

var a = parseInt('hello123'); 
alert(a); //NaN

NaN = 'hello123';
alert(NaN == 'hello123'); //true

var b = parseInt('hello123'); 
alert(b == 'hello123');  //?

-- Split --

本期最后一个问题:

<script>
function doClick() {
    alert(1);
}
</script>
<a href="#" onclick="执行:doClick();">测试</a>

上述的代码会执行(弹出 1)吗?详细描述请参看 http://www.hedgerwow.com/360/dhtml/js_label/

-- EOF --

这里是《Javascript 每日测试 - 外一篇》的 永久连接(Permalink),欢迎您 留言 或发送 Trackback。您也可以查看此篇文章的 Wap 版本(适用于移动设备)。 最后,如果您对本站的内容感兴趣,欢迎您 订阅本站
欢迎您留言

请输入您的称呼

请输入您的电子邮件

请您输入留言内容