给对象额外顶一个接口,如何访问对象里面的变量?

getList访问不到对象里面的arr table
var mod,b=0;
(function test(a){

  "use strict";
  a = a||{};  var arr=[],count= 0,table={};
  a.set=function(key,value,callback){
      arr.push(value);
      table[key]=value;
      count+=value;      if(callback)callback();
  };
  a.urlParse=function(url){      if(!(url.indexOf("?")>=0))return;      var str=url.split("?")[1].split(/&|=/),result={};      
  for(var i= 0,len=str.length;i<len;i+=2){
          table[str[i]]=str[i+1]-0;
          result[str[i]]=str[i+1]-0;
          count+=table[str[i]];
      }      return result;
  }
  a.get=function(key){      return table[key]?table[key]:"Don't has the value of"+key;
  }
  a.count=function(){      return count;
  }
  b=a;

})(mod);

b.getList=function(){

  var arrTable=[];  for(var i= 0,len=arr.length;i<len;i++){
      arrTable.push(arr[i]);
  }  for(var key in table){
      arrTable.push(key);
  }  return arrTable;

}


杨__羊羊
浏览 615回答 1
1回答

桃花长相依

<script type="text/javascript">&nbsp; &nbsp; var mod,b=0;&nbsp;(function test(a){&nbsp; "use strict";&nbsp; a = a||{};&nbsp; var arr=[],count= 0,table={};&nbsp; a.set=function(key,value,callback){&nbsp; &nbsp; &nbsp; arr.push(value);&nbsp; &nbsp; &nbsp; table[key]=value;&nbsp; &nbsp; &nbsp; count+=value;&nbsp; &nbsp; &nbsp; if(callback)callback();&nbsp; };&nbsp; a.urlParse=function(url){&nbsp; &nbsp; &nbsp; if(!(url.indexOf("?")>=0))return;&nbsp; &nbsp; &nbsp; var str=url.split("?")[1].split(/&|=/),result={};&nbsp; &nbsp; &nbsp; for(var i= 0,len=str.length;i<len;i+=2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[str[i]]=str[i+1]-0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[str[i]]=str[i+1]-0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count+=table[str[i]];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return result;&nbsp; }&nbsp; a.get=function(key){&nbsp; &nbsp; &nbsp; return table[key]?table[key]:"Don't has the value of"+key;&nbsp; }&nbsp; a.count=function(){&nbsp; &nbsp; &nbsp; return count;&nbsp; }a._arr = arr; // 把 局部 变量 arr 数组 赋值 到 a 对象 _arr 属性 里a._table = table; // 局部 table 也 一样&nbsp; b = a;})(mod);b.getList=function(){&nbsp; var arrTable=[],&nbsp; arr = this._arr,&nbsp; table = this._table;&nbsp; for(var i= 0,len=arr.length;i<len;i++){&nbsp; &nbsp; &nbsp; arrTable.push(arr[i]);&nbsp; }&nbsp; for(var key in table){&nbsp; &nbsp; &nbsp; arrTable.push(key);&nbsp; }&nbsp; return arrTable;};b.set('count',1); // 调用b.set('age',25,function(){alert('调用');});console.log(b.getList()); // [1,25,'count','age']&nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript