zoukankan      html  css  js  c++  java
  • js-语法

    js中slice方法(转)

    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属性).

  • 相关阅读:
    hostapd AP模式 2.4G/5G
    hostapd挂载不上驱动bcmdhd.ko以及SDIO读写错误
    C++入门 -- const用法(转载)
    C++ 入门 -- size_t
    C++ 入门 -- 全局变量的使用(转载)
    C++ 入门 -- const用法小结
    大数据揭示的10个常见JAVA编程错误
    jsfiddle
    ionic
    React native android 最常见的10个问题
  • 原文地址:https://www.cnblogs.com/awkflf11/p/10154751.html
Copyright © 2011-2022 走看看