代码的小小疑惑



 var Type={};
//type=["String","Array","Number"][i++]这不成了一个复合数组了吗??不太理解
for(var i=0,type;type=["String","Array","Number"][i++];){
    (function(type){
     Type["is"+type]=function(obj){
        return Object.prototype.toString.call(obj)==='[object]'+type+']';
     }
    })(type);
}
alert(Type.isArray([]));
alert(Type.isString("str"));

jquer JavaScript

BC245 9 years, 4 months ago
 
  type=["String","Array","Number"][i++]
 

这句话可以这样来理解


 var typeList = ["String", "Array", "Number"];
type = typeList[i];
i++;

音无结弦_ answered 9 years, 4 months ago

并不是。。
"String","Array","Number"]定义了一个三元的数组,["String","Array","Number"访问的是其第一个元素,即"String"。

纯天然大傻逼 answered 9 years, 4 months ago

type="String","Array","Number";

var arr = ["String","Array","Number"];
var j = i++;
var type= arr[j];

库洛。悠太 answered 9 years, 4 months ago

Your Answer