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]

原文地址:https://www.cnblogs.com/ZJTL/p/12580507.html

时间: 2024-10-01 02:56:35

Array 和 Array.prototype的相关文章

[C++] the pointer array & the array's pointer

void main(){ // the pointer array char* arry[] = { "hello", "world", "haha" }; for (int i = 0; i < 3; i++){ printf("string:%s,address:%p\n", arry[i], arry[i]); } // the array's pointer int a[10] = { 0, 1, 2, 3, 4

theme(&#39;item-list&#39;,array(&#39;items&#39;=&gt;array(&#39;aaa&#39;,&#39;bbb&#39;))) 是如何运行的 ?

$items['items'] = array( l('Configure', 'admin/config'), l('Structure', 'admin/structure'), ); $theme = theme('item_list', $items); 1,  判断是否所有的模块都加载了, if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) { throw new Exception(t('theme() may

JavaScript定义数组的三种方式(new Array(),new Array(&#39;x&#39;,&#39;y&#39;),[&#39;x&#39;,&#39;y&#39;])

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

Linux Array & Associative Array

Ordinary Array: Assign1: arrayName=(value1 value2 ...) Assign2: arrayName[index]=value Length: ${#arrayName[*]} or ${#arrayName[@]} Indexes: ${!arrayName[*]} or ${#arrayName[@]} Example: #!/bin/bash group1=(rio amos) group2[0]=bill group2[1]=frank gr

2018-6-3_《ES7的include、ES6的Array.of(),Array.from()及扩展符》

一. ES7的include //ES7,include-查找数组是否包含某个元素 返回布尔 let a=['OB','Koro1',1,NaN]; // let b=a.includes(NaN); // true 识别NaN // let b=a.includes('Koro1',100); // false 超过数组长度 不搜索 // let b=a.includes('Koro1',-3); // true 从倒数第三个元素开始搜索 // let b=a.includes('Koro1'

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

Array.of() &amp;&amp; Array()

Array.of(7) 创建一个具有单个元素 7 的数组, Array(7) 创建一个长度为7的空数组(注意:这是指一个有7个空位(empty)的数组,而不是由7个undefined组成的数组). Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3] Array.of(1); // [1] Array.of(1, 2, 3); //

81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此时可能存在nums[start]=nums[end]或者nums[start]=n

154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 思路:有重复元素的时候,不能按I中这么分三类(反例:Input: [3,3,1,3]=>将