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()
这两行代码输出都是:
xiaoming
xiaohong
那么他们的区别是什么? let name 这个变量存储在哪?
第一个代码段是因为闭包才使得输出不同吗?