Comments (10)
原型
题目:如何 JS 中的原型?
缺少了 '理解' 二字
from blog.
@SecondHandCode
原型
题目:如何 JS 中的原型?
缺少了 '理解' 二字
已修改
from blog.
执行上下文那好像说的不是很对
from blog.
@dadawanan
执行上下文那好像说的不是很对
哪里不对?
from blog.
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赋值对象还是可以读取对象(依旧没有清除)
from blog.
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赋值对象还是可以读取对象(依旧没有清除)
没问题呀,const
声明是地址不变的常量,重新赋值的操作会抛出 Assignment to constant variable
from blog.
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赋值对象还是可以读取对象(依旧没有清除)
没问题呀,
const
声明是地址不变的常量,重新赋值的操作会抛出Assignment to constant variable
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赋值对象还是可以读取对象(依旧没有清除)
没问题呀,
const
声明是地址不变的常量,重新赋值的操作会抛出Assignment to constant variable
from blog.
清理闭包方法
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
from blog.
我觉得你这博客的模板不错,想知道在哪下载的?
from blog.
@root-lucas 我觉得你这博客的模板不错,想知道在哪下载的?
vuepress官方模版,自行修改和定制插件即可
from blog.
Related Issues (20)
- 「评论」JavaScript版 · 剑指offer HOT 7
- 「评论」无声半年:面试、实习和生活总结 HOT 12
- 「评论」参与协作
- 「评论」redis热key,缓存穿透,击穿,集群等问题研究
- 「评论」FileAPI 文件操作实战
- 「评论」字体特效 HOT 3
- 「评论」输入框特效 HOT 3
- 「评论」按钮特效
- 「评论」Loader特效·基础篇
- 「评论」Loader特效·进阶篇
- 「评论」基于实时数据库:在线对战五子棋小游戏
- 「评论」实战篇:当Koa遇上Typescript的时候~
- 「评论」基础篇:TypeScript用法与实战 HOT 5
- 关于Gitalk和Vueress的问题! HOT 3
- 「评论」React Router 常见问题汇总
- Gitalk HOT 2
- 命令模式 es6实现
- 求16篇webpack教程 HOT 1
- 【提问】404汇总 HOT 2
- xin-tan老博客的那个技术栈目录很棒啊,但是找不到了
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.