数组的slice方法和类数组
来源:互联网 发布:java web 重新编译 编辑:程序博客网 时间:2024/06/05 04:22
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
slice
方法可以用来将一个类数组(Array-like)对象/集合转换成一个数组。你只需将该方法绑定到这个对象上。下述代码中 list 函数中的 arguments
就是一个类数组对象。
function list() { return Array.prototype.slice.call(arguments);}var list1 = list(1, 2, 3); // [1, 2, 3]
/*** Shim for "fixing" IE's lack of support (IE < 9) for applying slice* on host objects like NamedNodeMap, NodeList, and HTMLCollection* (technically, since host objects have been implementation-dependent,* at least before ES6, IE hasn't needed to work this way).* Also works on strings, fixes IE < 9 to allow an explicit undefined* for the 2nd argument (as in Firefox), and prevents errors when* called on other DOM objects.*/(function () { 'use strict'; var _slice = Array.prototype.slice; try { // Can't be used with DOM elements in IE < 9 _slice.call(document.documentElement); } catch (e) { // Fails in IE < 9 // This will work for genuine arrays, array-like objects, // NamedNodeMap (attributes, entities, notations), // NodeList (e.g., getElementsByTagName), HTMLCollection (e.g., childNodes), // and will not fail on other DOM objects (as do DOM elements in IE < 9) Array.prototype.slice = function (begin, end) { // IE < 9 gets unhappy with an undefined end argument end = (typeof end !== 'undefined') ? end : this.length; // For native Array objects, we use the native slice function if (Object.prototype.toString.call(this) === '[object Array]'){ return _slice.call(this, begin, end); } // For array like object we handle it ourselves. var i, cloned = [], size, len = this.length; // Handle negative value for "begin" var start = begin || 0; start = (start >= 0) ? start: len + start; // Handle negative value for "end" var upTo = (end) ? end : len; if (end < 0) { upTo = len + end; } // Actual expected size of the slice size = upTo - start; if (size > 0) { cloned = new Array(size); if (this.charAt) { for (i = 0; i < size; i++) { cloned[i] = this.charAt(start + i); } } else { for (i = 0; i < size; i++) { cloned[i] = this[start + i]; } } } return cloned; }; }}());
网上的习题:var slice = Array.prototype.slice; alert(slice.call({0: 'foo', length: 'bar'})[0]); // 当length值不能转化为有效的数字时,返回一个空数组alert(slice.call(NaN).length); //0 alert(slice.call({0: 'foo', length: '100'})[0]); // 返回一个长度为100的数组,第一个元素是‘foo’
var obj = {"2" : "a","3" : "b", "length" : 2, "push" : Array.prototype.push} obj.push('c');obj.push('d');console.log(obj);//Object {2: "c", 3: "d", length: 4, push: function}解释:
Array.prototype.push = function(target){this[this.length] = target;this.length++; }
类数组:属性必须是数字索引,且必须有length属性。
阅读全文
0 0
- 数组的slice方法和类数组
- 数组对象的slice和splice方法
- JavaScript的slice()和splice()数组方法
- 数组的slice()和splice()方法
- Js的数组方法对比:splice(),concat()和slice()
- js--打卡--12.04 数组的slice和splice方法
- js数组排序 reverse()和sort()方法的使用 复制数组slice()和克隆数组concat()
- JS数组的slice()方法传负数和字符串操作函数中的slice()、substr()、substring()
- javascript中数组对象的slice()方法
- Javascript数组---slice(),splice()方法的区别
- 数组 slice 和splice
- 数组中splice和slice的区别
- JavaScript数组slice方法详解
- Array.prototype.slice.call()详解及转换数组的方法
- Array.prototype.slice.call()详解及转换数组的方法
- Array.prototype.slice.call()详解及转换数组的方法
- go的数组与slice
- JS数组对象的splice()和slice()的区别
- eclipse设置自动提示
- 两个正整数之和
- MapListHandler:将多条记录封装到一个Map集合中;ColumnListHandler;将某列的值封装到List集合中,例如一列name属性等
- hexo+gitHub搭建个人独立博客(一)
- io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found
- 数组的slice方法和类数组
- CSS3背景
- Java 循环结构
- Python 3 笔记
- Ubuntu 14.04 解决VNC server 自启动
- nyoj1022 : 取石子(一)(巴什博弈)
- ScalarHandler: 将单个值封装、 例如select count(*),求内容的条数;KeyedHandler:将多条记录封装到一个Map集合的Map集合中。并且外面的Map集合是可以指定的
- CentOS 7 安装 zookeeper
- String类常用方法