如何从闭包中获得变量

Posted by Eleanor Mao on 2017-05-18

这里讲一个简单的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function foo(){
var person = {
"name": "joe",
"age": 18
}

return function getValue(name){
return person[name];
}
};

var bar = foo();
bar('name'); //=> 'joe'

Object.defineProperty(Object.prototype, 'self', {
get: function() {
return this;
}
});

bar('self'); //=> {name": "joe", "age": 18}

至于这么不让他通过扩展原型链获取数据,重写__proto__即可