zoukankan      html  css  js  c++  java
  • 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();
    
  • 相关阅读:
    ftp>ls 未连接
    Error running 'run': data.userName must not be null
    Excel另存为_有些Excel打开时会出现一些提示
    23种设计模式通俗理解
    清除html中的标记,只留下文字
    将DataTable中的数据导出成Excel
    C#读取文件
    系统性能瓶颈分析
    Angularjs的My97DatePicker扩展
    Memcache修改端口
  • 原文地址:https://www.cnblogs.com/pengsn/p/12690860.html
Copyright © 2011-2022 走看看