1、String.slice(start,end)
returns a string containing a slice, or substring, of string. It does not modify string。
slice()返回一个子片段,对原先的string没有影响,与subString的区别是,还可以用负数当参数,相当于是length+start,length+end.

Example:

var s = "abcdefg";
s.slice(0,4) // Returns "abcd"
s.slice(2,4) // Returns "cd"
s.slice(4) // Returns "efg"
s.slice(3,-1) // Returns "def"
s.slice(3,-2) // Returns "de"
s.slice(-3,-1) // Should return "ef"; returns "abcdef" in IE 4

2.Array.slice(start,end)

returns a slice, or subarray, of array. The returned array contains the element specified by start and all subsequent elements up to, but not including, the element specified by end. If end is not specified, the returned array contains all elements from the start to the end of array.
返回从start开始到end的子数组,如果end这个参数没有被设置,则返回从start开始到最后的数组元素。
Example:

var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]; buggy in IE 4: returns [1,2,3]

除了正常用法,slice 经常用来将 array-like 对象转换为 true array。在一些框架中会经常有这种用法。

Array.prototype.slice.call(arguments);//将参数转换成真正的数组.

因为arguments不是真正的Array,虽然arguments有length属性,但是没有slice方法,所以呢,Array.prototype.slice()执行的时候,Array.prototype已经被call改成arguments了,因为满足slice执行的条件(有length属性).

js中slice方法(转)的更多相关文章

  1. js中slice(),splice(),split(),substring(),substr()的使用方法和区别

    1.slice(): Array和String对象都有 在Array中  slice(i,[j]) i为开始截取的索引值,负数代表从末尾算起的索引值,-1为倒数第一个元素j为结束的索引值,缺省时则获取 ...

  2. 【转载】JS中bind方法与函数柯里化

    原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...

  3. 原生JS中apply()方法的一个值得注意的用法

    今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...

  4. js中apply方法的使用

    js中apply方法的使用   1.对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是: Object.extend = function(destinat ...

  5. paip.编程语言方法重载实现的原理及python,php,js中实现方法重载

    paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...

  6. js中settimeout方法加参数

    js中settimeout方法加参数的使用. 简单使用看w3school  里面没有参数调用,  例子: <script type="text/javascript"> ...

  7. js中split()方法得到的数组长度

    js 中split(",")方法通过 ”,“ 分割字符串, 如果字符串中没有 “,” , 返回的是字符串本身 var str = “abc”://分隔符个数为0 var newSt ...

  8. 关于JS中的方法是否加括号的问题

    js中的方法什么时候加括号什么时候不加括号呢,我们有时候经常就搞不清楚,记住下面这几点就好理解了. 1.函数做参数时都不要加括号. function fun(a){ alert(a); } funct ...

  9. js中的方法如何传入多个参数

    js中的方法如何传入多个参数 $(function () { let parameter1 = 1; let parameter2 = 'Hello World'; let parameter3 = ...

随机推荐

  1. restore not found的错误

    tensorflow保存模型后,restore的时候报参数not found是什么原因呢 一般预测的流程是:建图然后restore参数,很有可能你的变量作用域和train的时候不一样,那么在现在的变量 ...

  2. SQL列转行用逗号隔开

      declare @result varchar(255) set @result = ” select @result = @result + cast(F_IT_FWID as varchar( ...

  3. webstorm crack

    webstorm 2018.2注册码如下:{"licenseId":"ThisCrackLicenseId","licenseeName": ...

  4. LCA最近公共祖先模板代码

    vector模拟邻接表: #include<iostream> #include<cstdio> #include<cstring> #include<cma ...

  5. Binary Tree Maximum Node

    Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ ...

  6. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  7. JavaScript的使用你知道几种?(上)

    往期回顾 在上一期的<JavaScript的组成 | DOM/BOM>☜里,我们有对文档对象模型-DOM.浏览器对象模型-BOM 这两大部分进行了解学习,如果有还不是很明白的小伙伴们,可以 ...

  8. web roadmap

  9. Python 测试多进程的时间

    import time from multiprocessing import Process def f1(): time.sleep(2) print("子进程1号") def ...

  10. TX锁处理

    实际处理后,在测试环境中模拟还原TX锁,及处理. 本篇博客目录: 1.TX锁模拟实际环境 2.登陆数据库,查询相关信息 3.确认锁源头,kill进程释放资源 一.TX锁模拟 sess_1 SQL> ...