If you are familiar with Python, you will know that slicing in Python greatly simplifies operations. If you look back at the JS slicing family, there are still one or two of them, slice, splice, substr, substring, etc. There are some people who have basic support for the rest of the work.

[: :] operator

JavaScript based syntax, if you write this, then you should try, (; ¬ _ ¬), here we use the way of function to achieve the slice effect.

Go straight to code (array)

if (!Array.prototype.select) {
	    Object.defineProperty(Array.prototype. 'select'. {
	        value: function(item) {
	            'use strict';
	            if (this = = null) {
	                throw new TypeError('Array.prototype.select called on null or undefined');
	            }
	            if (!Array.isArray(this)) {// This must be an Array type
	                throw new TypeError('This Object is not Array');
	            }
	            var arr = [];
	            var i = arguments[0] || 0;// Start position
	            var len = this.length >>> 0;// The length of the array
	            var end = len;// End position
	            if (!arguments[1]) {
	                if (arguments[1] = = = 0) {// Resolve the case where the end position is 0
	                    end = 0;
	                }
	            }else{
	                end = Math.min(Math.abs(arguments[1]), len);
	                // If the end position is too long, set it to the end position of the array
	            }
	            end = end > = 0 ? end : len + end;// End position is negative
	            var step = arguments[2] || 1;// The default value is consecutive
	            step = Math.abs(step);
	            if (arguments[2] < 0) {// Reverse values
	                for (i = end - 1; i > = 0; i = i - step) {
	                    arr.push(this[i]);
	                }
	            }else {
	                for (i; i < end; i = i + step) {
	                    arr.push(this[i]);
	                }
	            }
	            return arr;
	        },
	        configurable: true.
	        enumerable: false.
	        writable: true.
	    });
}
Copy the code

About arrays

if (!Array.isArray) {
    Object.defineProperty(Array. 'isArray'. {
        value: function(arg) {
            return Object.prototype.toString.call(arg) = = = '[object Array]';
        },
        configurable: true.
        enumerable: false.
        writable: true
    });
}
Copy the code

Slicing of strings

if (!String.prototype.select) {
	    Object.defineProperty(String.prototype. 'select'. {
	        value: function(item) {
	            'use strict';
	            if (this = = null) {
	                throw new TypeError('String.prototype.select called on null or undefined');
	            }
	            if (typeof this ! = = "string") {
	            	throw new TypeError('This Object is not String');
	            }
	            var arr = [];
	            var i = arguments[0] || 0;// Start position
	            var len = this.length >>> 0;// The length of the array
	            var end = len;// End position
	            if (!arguments[1]) {
	                if (arguments[1] = = = 0) {// Resolve the case where the end position is 0
	                    end = 0;
	                }
	            }else{
	                end = Math.min(Math.abs(arguments[1]), len);
	                // If the end position is too long, set it to the end position of the array
	            }
	            end = end > = 0 ? end : len + end;// End position is negative
	            var step = arguments[2] || 1;// The default value is consecutive
	            step = Math.abs(step);
	            if (arguments[2] < 0) {// Reverse values
	                for (i = end - 1; i > = 0; i = i - step) {
	                    arr.push(this[i]);
	                }
	            }else {
	                for (i; i < end; i = i + step) {
	                    arr.push(this[i]);
	                }
	            }
	            return arr.join(' ');
	        },
	        configurable: true.
	        enumerable: false.
	        writable: true.
	    });
}
Copy the code

Test the

var arr = [1.2.3.4.5.6.7.8.9];
console.log(arr.select(1.-1.2));
console.log(arr.select(1.-1.-2));
Copy the code

According to the results
Performance comparison, or native ah

Source: slice operation, and some other array operations