-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
「评论」JavaScript基础知识梳理(下) #56
Comments
原型 |
已修改 |
执行上下文那好像说的不是很对 |
哪里不对? |
function mockData() {
const mem = {name:"lucas",age:22};
return {
clear: ()=>{mem = null}, // 显式暴露清理接口
get: page => {
if (page in mem) {
return mem[page];
}
mem[page] = Math.random();
}
};
}
console.log(mockData().get('name')); //lucas
mockData().clear();
console.log(mockData().get('name')); //lucas 执行到mockData().clear();直接报错Assignment to constant variable,即便换了let var赋值对象还是可以读取对象(依旧没有清除) |
没问题呀, |
|
清理闭包方法 function mockData() {
const mem = {name:"lucas",age:22};
return {
clear: () => {
for(let i in mem){
delete mem[i];
}
}, // 显式暴露清理接口
get: (page) => {
if (page in mem) {
return mem[page];
}
mem[page] = Math.random();
}
};
}
var result = mockData();
console.log(result.get('name')); //lucas
result.clear(); //清理常量对象
console.log(result.get('name')); //undefined |
我觉得你这博客的模板不错,想知道在哪下载的? |
vuepress官方模版,自行修改和定制插件即可 |
JavaScript基础知识梳理(下):https://xin-tan.com/passages/2019-03-27-javascript-second/
The text was updated successfully, but these errors were encountered: