-
js中的变量提升和函数提升
console.log(tmp); #undefined var tmp = 123; console.log(tmp); #123 #其实上边代码按照下边执行 var tmp; console.log(tmp); tmp = 123; console.log(tmp); #函数提升 console.log(f1); #function() {} console.log(f2); #undefined function f1() {} var f2 = function() {}
-
使用var声明的变量和不使用的情况
function c() { a = 123; //相当于window.a = 123; } c(); alert(a); //123 function d() { var b = 456; } d(); alert(b); //报错
-
关于ES5的call和apply
var cat = function() { this.voice = "miaomiao"; this.food = "yu"; this.eat = function(){ console.log('eating ' + this.food); } } var dog = function() { this.voice = "wangwang"; this.food = "gutou"; this.miao = function(voice1, voice2){ console.log(this.voice); if(typeof voice1 !== "undefined") { console.log(voice1); } if(typeof voice2 !== "undefined") { console.log(voice2); } } } c = new cat(); d = new dog(); c.eat(); d.miao(); c.eat.call(d); d.miao.call(c, "wang1", "wang2"); d.miao.apply(c, ["wang1", "wang2"]); t = c.eat.bind(d); t(); //gutou
网友评论