<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// 闭包的使用
var o = (function () {
var obj = {
a: 12,
b: '65',
c: ['1', 2],
d: {
d1: 1,
d2: '2'
}
}
return {
get: function (k) {
// 判断一下
if (obj.hasOwnProperty(k)) {
return obj[k]
} else {
return undefined
}
}
}
})();
console.log(o.get('d'));
// 未加判断时有漏洞
// Object.defineProperty(Object.prototype, 'abc', {
// get() {
// return this;
// }
// })
// var o2 = o.get('abc');
// o2.a = 123213;
// console.log(o2);
</script>
</html>
标签:闭包,return,get,o2,应用,var,obj,实际
From: https://www.cnblogs.com/lbx6935/p/16845987.html