文章详情页
javascript - 为什么getValue()得出的是123?
问题描述
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. angular.js - 请教angularjs里html和js通信的问题2. angular.js - 我想实现打印angular $scope数据,代码如何实现呢?求教(如下图)3. angular.js - angular2动态显示隐藏组件问题4. angular.js - 用angular2-cli打包项目之后,跳转路由刷新会报404的错误5. angular.js - angular-ueditor使用中,插入图片,加载完成后不更新angularmodel的问题;6. angular.js - 在用会话存储传递参数时,使用angularjs冲突7. jquery - angularjs的数据为什么渲染不到页面上?数据获取到了,我的代码哪里出了错了?8. node.js - 怎么在初始化的时候使得一个子组件不加载?9. python - 如何在docker上部署pyspider10. 新人做网站求帮助
排行榜