js中的一些函数问题

TEST1

function Foo(){
		getName = function(){
			alert(1);
		}
		return this;
	}	
	var getName = function(){
		alert(4);
	}
	Foo().getName(); //1 

解析:
声明提前后再分析代码。

 var getName;
 function Foo(){
 	getName = function(){
 		alert(1);
 	}
 	return this;
 }	
 getName = function(){
 	alert(4);
 }
Foo().getName();

Foo( )返回的是指向window的this
this.getNam( )查找的是window中属性为getName的函数
getName在Foo( )时被重新设置,故输出为1。

TEST2

 var getName = function (){
	 	alert(4);
	 }
	 function getName() {
	 	alert(5);
	 }
	 getName(); // 4

解析:函数声明提前后再分析

var getName;
function getName(){
	 alert(5);
}
getName = function(){
	alert(4);
}
getName();

将函数声明提前,function声明也提前了,相同名字的直接覆盖,故输出4。

TEST3

function foo(){
		foo.a = function(){
			alert(1);
		}
		this.a = function(){
			alert(2);
		}
		a = function(){
			alert(3);
		}
		var a = function(){
			alert(4);
		}
	}
	foo.prototype.a = function(){
		alert(5);
	}
	foo.a = function(){
		alert(6);
	} 
	foo.a(); //6
	var obj = new foo();
	obj.a(); //2
	foo.a();  //1

将代码进行改写

function foo(){
	foo.a = function(){
 		alert(1);
 	}
 	this.a = function(){
 		alert(2);
 	}
 }
 foo.prototype.a = function(){
 	alert(5);
 }
 foo.a = function(){
 	alert(6);
 }
 foo.a();
 var obj = new foo();
 obj.a();
 foo.a(); 

第一次foo.a()调用的外层函数,输出为6;
foo实例化创建obj,实例化优先调用this.a,故输出为2;
再调用foo.a()时为1;

TEST4

var name = 'lili';
	var obj = {
		name: 'liming',
		prop:{
			name:'ivan',
			getName:function(){
				return this.name;
			}
		}
		console.log(obj.prop.getName()); //输出 ivan 此时this指向了prop里存放的对象
		var test = obj.prop.getName;
		console.log(test());//输出 lili 此时this指向了全局对象window
	}

TEST5

var day = "Valentine's Day";
	var object = {
		day : 'the Lantern Festival',
		getDay : function(){
			return function(){
				return this.day;
			}
		}
	}
	alert(object.getDay()());// Valentine's Day

object.getDay( )返回的是一个函数
函数执行,this指向window,所以返回的是Valentine’s Day;

TSET6

var i = 10;
function test(){
	i = 20;
	console.log(i); //20
	for(var i = 0; i < 6; i++){
		console.log(i); // 0 1 2 3 4 5
	}
	console.log(this.i); // 10
	console.log(i); //6
}
test();
console.log(i); //10

解析:
1、由于给局部变量i赋值,所以输出为20
2、for循环输出0、1、2、3、4、5,当i = 6终止循环
3、再一般函数中使用this指代全局对象,故输出为10;
4、for循环结束后,i=6 ,故输出为6
5、输出全局变量 i= 10;
TEST7

var user = {
		var user = {
		count:1,
		getCount : function(){
			return this.count;
		}
	}
	console.log(User.getCount()); //1
	var res = User.getCount;
	console.log(res()); // undefined

解析:
getCount函数被user对象调用 ,this指向user
res 变量接收的是函数体
res执行时 this指向window window 中没有count 所以返回undefined

TEST8

for(var i = 0; i < 4; i++){
		setTimeout(function(){
			console.log(i); //4 4 4 4
		},1000);
	}

js首先执行主线程,异步相关的会存储在异步队列
主线程执行后 i = 4;

TEST9

var test = (function(a){
		this.a = a;
		return function(b){
			console.log(a,b); // 1 4
			return this.a + b; // 1+4
		}
	})(function(a,b){
		console.log(a,b);//1 2
		return a;
	}(1,2));
	console.log(test(4)); //5

	// 立即执行函数传参
	// 先传参 1 ,2  return a = 1 ;
	// a = this.a = 1;
	// 传入 test(4) b = 4
	// return this.a + b = 1 + 4 

TEST10

function fun(a){
		console.log(a);//1
		var a = 123;
		console.log(a); //123
		(function a(){
			var a = 100;
			return this.a;
		})();  
		console.log(b); //undefined
		var b = function(){
			console.log(a);
		}
		console.log(b);//fun
	}
	fun(1);
	// 解析:
	// AO{
	// 	a:undefined
	// 	b:undefined
	// }
	// AO{
	// 	a:1
	// 	b:undefined
	// }
	// AO{
	// 	a:123;
	// 	b:undefined
	// }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值