Array和 Array.prototype 的定义
Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象
Array.prototype 属性表示 Array 构造函数的原型,并允许您向所有Array对象添加新的属性和方法。
获取相应的属性名称
Object.getOwnPropertyNames(Array)
//[ "length", "name", "prototype", "isArray", "from", "of" ]
Object.getOwnPropertyNames(Array.prototype)
//[ "length", "constructor", "concat", "copyWithin", "fill", "find", "findIndex", "pop", "push",
"reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "keys", "entries",
"forEach", "filter", "map", "every", "some", "reduce", "reduceRight", "toString", "toLocaleString",
"join", "lastIndexOf", "values", "flat", "flatMap" ]
- Array是一个 function 对象,是JS的内置对象。js中所有的数组方法均来自于Array.prototype,和其他构造函数一样,你可以通过扩展 Array 的 prototype 属性上的方法来给所有数组实例增加方法
Array.prototype 的用法
-
给 Array 对象添加新的方法
Array.prototype.duplicator = function() { let s = this.concat(this) return s } let t = [1,2,3,4,5].duplicator() console.log(t) // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
-
JS合并两个数组的方法
var array = ["football", "basketball"]; var array2 = ["volleyball", "golfball"]; var i = Array.prototype.push.apply(array,array2); console.log(array); // ["football", "basketball", "volleyball", "golfball"] console.log(i); // 4
网友评论