文章详情页
javascript - 为什么getValue()得出的是123?
浏览:179日期:2023-05-16 13:41:04
问题描述
var getValue,setValue; (function(){ var secret=0; getValue=function(){return secret; }; setValue=function(v){if(typeof v==='number'){ secret=v; } }; }()); getValue();//0 setValue(123); getValue();//123 setValue(false); getValue();//123
问题解答
回答1:getValue()和setValue()为闭包, 共享一个变量 secret, 所以在setValue函数中更改了secret,getValue()读取secret当然会随之改变.
回答2:不是为0么,你怎么得出的123.
标签:
JavaScript
相关文章:
1. javascript - nginx 反向代理 js跨域问题?2. javascript - 百度地图一开始设置了中心点,怎么通过按钮在调转到对应的中心点????3. javascript - Object.create(null) 和 {} 区别是什么4. css - width设置为100%之后列表无法居中5. javascript - 呈现引擎是什么?6. objective-c - iOS中rangeOfMisspelledWordInString这个方法有什么用呢7. python for循环中的函数只能运行一次?8. javascript - vue中自定义事件如何传递参数?9. javascript - 前端地图动画效果如何实现。10. javascript - 关于vue组件之间的通信
排行榜

网公网安备