日期:2014-05-16 浏览次数:20349 次
function add(number) { alert(number + 10); } function add(number) { alert(number + 20); } add(20); // 40
var add = new Function("number","alert('hello')"); var add = new Function("number","alert('world')");
function test() { alert(""); } alert(test()); //undefined 没有返回值 alert(test); //打印函数的源代码
var s ; alert(s); //undefined alert(s2); //s2没有定义,报错
var s ; alert(typeof s); //undefined alert(typeof s2); //undefined //null与undefined的关系: undefined实际上从null派生而来 alert(undefined == null)
function test() { s = "hello"; } test(); alert(s); //hello
function test() { var s = "hello"; } alert(s); // s没有定义,
//js的作用域 function f(props) { for(var i=0; i<10; i++) {} alert(i); //10 虽然i定义在for循环的控制语句中,但在函数 //的其他位置仍旧可以访问该变量. if(props == "local") { var sco = "local"; alert(sco); } alert(sco); //同样,函数仍可引用if语句内定义的变量 } f("local"); //10 local local var sco = "global"; function print1() { alert(sco); //global } function print2() { var sco = "local"; alert(sco); //local } function print3() { alert(sco); //undefined var sco = "local"; alert(sco); local } print1(); //global print2(); //local print3(); //undefined local
function print3() { var sco; alert(sco); sco = "local";