深入浅出的JavaScript 面向对象的特性
javascript 王者归来
javascript不支持函数或方法的重载,只会覆盖
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); //undefinedalert(s2); //s2没有定义,报错
var s ;alert(typeof s); //undefinedalert(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"; alert(sco); }
var scope = "global" //定义全局变量 function print() { alert(scope); } function change() { var scope = "local"; //定义局部变量 print(); //虽然是在change函数的作用域内调用print函数, //但是print函数执行时仍旧按照它定义时的作用域起作用 } change(); //golbal