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
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
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- 深入理解PHP之匿名函数
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 设计模式之创建型模式 - 特别的变量问题
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法