猿问

新手:关于函数对象

这样写是对的

Smart猫小萌
浏览 547回答 1
1回答

慕后森

var byteArrayUntil=(function(){&nbsp; &nbsp;getUseData=function(data,offset){&nbsp; &nbsp;//敲黑板!!!划重点!面试必考题! 声明变量不写var,就等于 window.getUseData&nbsp; &nbsp; &nbsp; &nbsp; var arr=[];&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<=799;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.push(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return arr;&nbsp; &nbsp; }&nbsp; &nbsp;getUseJson=function(data,offset){&nbsp; &nbsp; &nbsp; &nbsp; var arr=[];&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0;i<=500;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.push(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return {'hz':arr};&nbsp; &nbsp; }return this; //这里this是什么呢?是运行的上下文,这是个立即执行函数,是谁执行了它捏?是window,所以这里的this就是window 对象,辣么var byteArrayUntil = 这个return的this,辣么,byteArrayUntil 就指向了window。})();console.log(byteArrayUntil.getUseJson()); // 所以,byteArrayUntil.getUseJson() 就等于 window.getUseJson()同样道理,下面那个报错的代码块this依然等于window,this.getUseJson就等于window.getUseJson和上一题一样但是,byteArrayUntil在这里啥都不是啊。。因为立即执行函数木有return,它就是个undefined。so,undefined.getUseJson()必然报错了。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答