走进javascript(六)

函数表达式,函数声明; 函数声明整体提升

递归

M1:
	arguments.callee(); //调用自身,但在严格模式下无效
	
M2:
	命名函数表达式
	var fun2 = (function fun() {
			...
			fun();
			...
	});

闭包

有权访问另一个函数作用域中的变量的函数

for(var i=0;i<10;i++) {
	(function(j){
		setTimeout(function(){
			console.info(j);
		}, 1000);
	})(j);
}

模仿块级作用域

if white for 声明的变量并不只存在于当前块级范围内

for(var i=0;i<10;i++) {
	console.info(i);
}
alert(i);

模仿块级作用域
(function(){
	for(var i=0;i<10;i++){
		console.info(i);
	}
})();
alert(i);

This

The this keyword is relative to the execution context, not the declaration context.

By default, this refers to the global object.

为什么说是全局对象(the global object),因为非浏览器情况下(例如:nodejs)中全局变量并非window对象,而就是叫“全局变量”(the global object)。不过由于我们这片文章主要讨论的是前端开发知识,所以nodejs就被我们忽略了。

在浏览器中setTimeout、setInterval和匿名函数执行时的当前对象是全局对象window

var name = 'window-name';
function Item() {
	this.name ='zhangsan';
	this.display = function() {
		console.info(this.name);
	};
	this.show = function() {
		setTimeout(this.display, 1000);
	}
}

var item = new Item();
item.show();
item.display();

猜你喜欢

转载自www.cnblogs.com/pengsn/p/12690860.html