代码:
<script type="text/javascript" charset="utf-8"> // 伪数组(ArrayLike) var jqObj = { 0: 'one', 1: 'two', 2: 'three', length: 3 }; //数组 ary = ['one', 'two', 'three']; // 将伪数组(ArrayLike)转成数组 function jqObjToArray(json){ var slice = Array.prototype.slice; return slice.call(json, 0); } // 将数组转成伪数组(ArrayLike) function ArrayToJqObj(ary){ var obj = {}, push = Array.prototype.push; push.apply(obj, ary); return obj; } console.log(jqObjToArray(jqObj)); console.log(ArrayToJqObj(ary)); </script>
return slice.call(json, 0); 输出["one","two","three"]
return slice.call(json, 1); 输出["two","three"]
这个他内部运行的原理??为什么
还有这个
push.apply(obj, ary);
注 :不是问apply和call 个差别 这个就不用回答了!
叮当猫咪
相关分类