当前位置:编程学习 > JS >>

关于JavaScript的编程题

熟悉各种数据类型、常量、变量以及之间的转换过程。请设计几个程序分别能够实现以下功能。
(1)根据不同的定义方式,能够在屏幕上输出null和undefined。
(2)设计一个显性转换的例子,表明布尔常量true在字符串环境下可以隐式转换为“true”;在数字环境下可以转换为1.
(3)设计二个例子,说明局部变量在整个函数体内有效,以及嵌套函数的有效范围。
答案:
// (1)
var undefValue;
document.write(undefValue); //输出undefined

document.write("<br/>"); //换行

var nullValue = null;
document.write(nullValue); //输出null

document.write("<br/>"); //换行


//(2)
var str = "This is " + true; //在字符串环境下将true隐式转换为"true"
document.write(str); //输出 This is true.

document.write("<br/>"); //换行

var num = 0 + true; //在数字环境下将true隐式转换为1
document.write(num); //输出 1

document.write("<br/>"); //换行


//(3)
function TestLocalVar(){
for(var i=0; i<1; i++){
var localVar = 100;
}

document.write(localVar); //输出 100

document.write("<br/>"); //换行

}
TestLocalVar();


function TestNestedFunction(){
function ANestedFunction(){
document.write("This is a Nested function.");

document.write("<br/>"); //换行
}

ANestedFunction();
}

//调用TestNestedFunction以间接地调用ANestedFunction,输出显示调用成功
TestNestedFunction();


//直接调用ANestedFunction, 发生异常,说明超出了ANestedFunction的作用范围。
try { ANestedFunction(); } catch (e) {
document.write(e);

document.write("<br/>"); //换行
}

上一个:Javascript 为什么要用权重算法
下一个:javascript保存数据代码??

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,