您的位置:首页技术文章
文章详情页

javascript - 为什么getValue()得出的是123?

【字号: 日期:2023-05-16 13:41:04浏览:42作者:猪猪

问题描述

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
相关文章: