您的位置:首页 > Web前端 > JavaScript

javascript 闭包(closure) 与匿名函数的this

2016-03-22 21:47 483 查看
//使用外部函数传进来的参数的函数 ---> 闭包是指有权访问另一个函数作用域中的变量的函数。

function createFunctions(){

  var result=new Array();

  for(var i=0;i<3;i++){

  (function(i){

     result[i]=function(){

        return i;

     };

  })(i)

  }

  return result;

}

console.log(createFunctions()[0]());

consol

匿名函数的执行环境具有全局性, this 指向 window

var name="the window";

var obj={

   name:"the obj",

   getNameFunc:function(){

       //console.log(this==obj);

       return function(){

       // 匿名函数

         console.log(this.name);

     }

  }

}

obj.getNameFunc()();// the window

prototype是函数的特有属性,比如说

function Person(name, age){
this.name = name;
this.age = age;

this.getInfo = function(){
console.log(this.name + " is " + this.age + " years old");
};
}

var will = new Person("Will", 28);

will.prototype// undefinded
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript 原型 闭包