function Student(n) { let name = n; this.say = function () { console.log(name)
}
} let xiaoming = new Student('xiaoming') let xiaohong = new Student('xiaohong')
xiaoming.say()
xiaohong.say() function Student(n) { this.name = n; this.say = function () { console.log(this.name)
}
} let xiaoming = new Student('xiaoming') let xiaohong = new Student('xiaohong')
xiaoming.say()
xiaohong.say()這兩行代碼輸出都是:xiaomingxiaohong那么他們的區(qū)別是什么? let name 這個變量存儲在哪?第一個代碼段是因為閉包才使得輸出不同嗎?
這種結(jié)構(gòu)可以叫閉包嗎?
三國紛爭
2019-03-03 14:27:37