js 小练习题
<script> /*1.结论,IIFE中运行顺序3,1,执行test(4),会传递参数*/ /*var a=5; var test = (function(a){ console.log(1); console.log("a:",a); this.a = a; return function(b){ console.log(2); console.log("b:",b); return this.a+b; } }(function(a,b){ console.log(3); console.log("a:",a,"b:",b); return a; }(1,2))); console.log(test(4)); var test1 = (function(c){ console.log("c:",c); return function(d){ console.log("d:",d,"arguments:",arguments); return d; }; }(1)); console.log(test1(5,7,8)) //d: 5 arguments: [5, 7, 8] */ /*2 这里需要注意 函数变量的提升覆盖 第一个会被覆盖*/ /*var x = 1,y = z = 0; function add(n){ return n=n+3; } y = add(x); function add(n){ return n=n+1; } z = add(x); console.log(x); console.log(y); console.log(z); */ /*3 */ function People(){ this.name = 'Tim'; this.sayHello = function(){ console.log('People:'+this.name); } } function Man(name){ this.name = name; this.sayHello = function(){ console.log('Man:'+this.name); } } Man.prototype = new People(); var man = new Man('Jim'); man.sayHello(); People.call(man); man.sayHello(); </script>
<script>
/*1.结论,IIFE中运行顺序3,1,执行test(4),会传递参数*/
/*var a=5;
var test = (function(a){
console.log(1);
console.log("a:",a);
this.a = a;
return function(b){
console.log(2);
console.log("b:",b);
return this.a+b;
}
}(function(a,b){
console.log(3);
console.log("a:",a,"b:",b);
return a;
}(1,2)));
console.log(test(4));
var test1 = (function(c){
console.log("c:",c);
return function(d){
console.log("d:",d,"arguments:",arguments);
return d;
};
}(1));
console.log(test1(5,7,8)) //d: 5 arguments: [5, 7, 8]
*/
/*2 这里需要注意 函数变量的提升覆盖 第一个会被覆盖*/
/*var x = 1,y = z = 0;
function add(n){
return n=n+3;
}
y = add(x);
function add(n){
return n=n+1;
}
z = add(x);
console.log(x);
console.log(y);
console.log(z); */
/*3 */
function People(){
this.name = 'Tim';
this.sayHello = function(){
console.log('People:'+this.name);
}
}
function Man(name){
this.name = name;
this.sayHello = function(){
console.log('Man:'+this.name);
}
}
Man.prototype = new People();
var man = new Man('Jim');
man.sayHello();
People.call(man);
man.sayHello();
</script>