日期:2014-05-16 浏览次数:20503 次
function outerfunc() { var mvar = 1; function innerFunc() { if (mvar == undefined) var mvar = 100; console.log(mvar); }; innerFunc(); } outerfunc();
function outerFunc() { mvar = 1; function innerFunc() { if (mvar == undefined) mvar = 100; console.log(mvar); }; innerFunc(); } outerFunc();
function outerfunc() { var mvar = 1; function innerFunc() { if (mvar == undefined) var _mvar = 100; else var _mvar = mvar; console.log(_mvar); }; innerFunc(); } outerfunc();
function outerfunc() { var mvar = 1; function innerFunc() { if (mvar == undefined) var mvar = 100;//如果你是要操作outerfunc的私有变量mvar,不要使用var定义mvar,要不这个mvar是innerFunc的变量了 console.log(mvar);//这里也是innerFunc的变量mvar }; innerFunc(); } outerfunc();
------解决方案--------------------
if (typeof mvar == "undefined")
mvar = 100;