1. 程式人生 > >js建構函式記憶體在的閉包

js建構函式記憶體在的閉包

function Func(x) {
this.x = x;
this.print=function() {
console.info(this.x);
(function (){
console.info(x);
})();
}
}
var a = new Func(30);
console.dir(a);
a.age = 300;
console.dir(a.print());//300,30
//存在於建構函式內的閉包 age:30 發生了閉包
//this.print 記憶體在閉包,閉包發生在構造new的時候,函式構造後記憶體銷燬了,但是屬性存在了.