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对象添加新的方法

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]

猜你喜欢

转载自www.cnblogs.com/ZJTL/p/12580507.html