What's the point of .slice(0) here?

前端 未结 5 812
后悔当初
后悔当初 2020-12-23 18:31

I was studying the jQuery source when I found this (v1.5 line 2295):

namespace = new RegExp(\"(^|\\\\.)\" +
  jQuery.map( namespaces.slice(0).sort(), fcleanu         


        
5条回答
  •  [愿得一人]
    2020-12-23 19:24

    In addition to what @Anon said:

    The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

    Example1:

    var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
    var citrus = fruits.slice(1, 3);
    

    The result of citrus will be:

    Orange,Lemon
    

    Example2:

    var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
    var citrus = fruits.slice(-3, -1);
    

    The result of citrus will be:

    Lemon,Apple
    

    Further information can be found here.

提交回复
热议问题