We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
闭包的两个特点:
总的来说,函数闭包决定了:闭包所对应的函数代码如何访问数据,以及闭包内的数据何时销毁。
// 没有函数实例产生 function myFunc(){ } var f1 = myFunc; var f2 = myFunc; alert(f1 === f2)
function MyObject(){ } MyObject.prototype.method = function(){} var obj1 = new MyObject(); var obj2 = new MyObject(); alert( obj1.method === obj2.method) // 对象的实例只持有原型中的方法的一个引用,因为也不产生(方法)函数的实例。
function MyObject(){ this.method = function(){} } var obj1 = new MyObject; var obj2 = new MyObject; alert( obj1.method === obj2.method) //false
//构造器函数 function MyObject(){ var instance_data = 100; this.getInstanceData = function(){ return instance_data; } this.setInstanceData = function(v) { instance_data = v; } } // 使用一个额匿名函数去修改构造器的原型 MyObject.prototype,以访问该匿名函数中的 upvalue void function(){ var class_data = 5; this.getClassData = function(){ return class_data; } this.setClassData = function(v){ class_data = v; } }.call(MyObject.prototype); var obj1 = new MyObject(); var obj2 = new MyObject(); // obj1 与 obj2 的 getInstance 是不同函数实例,因此访问的是不同闭包的 upvalue obj1.setInstanceData(10); console.log(obj2.getInstanceData()); // obj1 与 obj2 的 getClassData 是同一个函数实例,因此在访问相同 的 upvalue. obj1.setClassData(20); console.log(obj2.getClassData());
function aFunc(){ function MyFunc(){} return myFunc; } var f1 = new aFunc(); var f2 = new aFunc(); console.log(f1===f2) //FALSE
//foo & bar 产生函数实例 function foo(){ var MyFunc = function(){} return MyFunc; } function bar(){ return function(){ }; }
//返回同一个实例 var aFun3 = function(){ var foo = function(){ console.log(111) }; return function(){ return foo; } }() var f3 = aFun3(); var f4 = aFun3(); console.log(f3 === f4)
调用对象:
“调用对象”的局部变量维护规则
“全局对象”的变量维护规则
函数闭包 与 “调用对象”的生存周期
在运行期改函数实例有一个函数闭包,在执行时,引擎会:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
闭包的两个特点:
总的来说,函数闭包决定了:闭包所对应的函数代码如何访问数据,以及闭包内的数据何时销毁。
调用对象:
“调用对象”的局部变量维护规则
“全局对象”的变量维护规则
函数闭包 与 “调用对象”的生存周期
在运行期改函数实例有一个函数闭包,在执行时,引擎会:
The text was updated successfully, but these errors were encountered: