悟透前端 | 參悟Javascript中的call和apply

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於前端工程師來說,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":"算是常用的函數方法,允許通過函數和在函數調用中指定","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":"的指向。那麼這兩個方法到底有什麼區別呢?本文將詳細介紹這兩個方法,順便加深對其理解。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"call","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"方法使用一個指定的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 值和單獨給出的一個或多個參數來調用一個函數。允許爲不同的對象分配和調用屬於一個對象的函數/方法。提供新的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"this ","attrs":{}},{"type":"text","text":"值給當前調用的函數/方法。你可以使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 來實現繼承:寫一個方法,然後讓另外一個新的對象來繼承它(而不是在新對象中再寫一次這個方法)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const obj = {\n domain: \"www.infoq.cn\",\n};\nfunction siteurl(protocol, path) {\n console.log([protocol, this.domain, path].join(\"\"));\n}\nsiteurl.call(obj, \"https://\", \"/u/devpoint\"); // https://www.infoq.cn/u/devpoint","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"語法","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"function.call(thisArg, arg1, arg2, ...)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"thisArg","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":":可選的。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"function","attrs":{}}],"marks":[{"type":"italic"}],"attrs":{}},{"type":"text","text":" 函數運行時使用的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 值。請注意,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":"可能不是該方法看到的實際值:如果這個函數處於非嚴格模式下,則指定爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":" 或 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":" 時會自動替換爲指向全局對象,原始值會被包裝。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"arg1, arg2, ...","attrs":{}}],"attrs":{}},{"type":"text","text":":指定的參數列表。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"返回值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用調用者提供的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 值和參數調用該函數的返回值。若該方法沒有返回值,則返回 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"apply","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"apply()","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","text":" 方法調用一個具有給定","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":"值的函數,以及作爲一個數組(或類似數組對象)提供的參數。在調用一個存在的函數時,你可以爲其指定一個 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 對象。 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 指當前對象,也就是正在調用這個函數的對象。 使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":", 可以只寫一次這個方法然後在另一個對象中繼承它,而不用在新對象中重複寫該方法。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"語法","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"func.apply(thisArg, [argsArray]);","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"thisArg","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":":必選的。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"marks":[{"type":"italic"}],"attrs":{}},{"type":"text","text":" 函數運行時使用的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 值。請注意,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":"可能不是該方法看到的實際值:如果這個函數處於非嚴格模式下,則指定爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":" 或 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":" 時會自動替換爲指向全局對象,原始值會被包裝。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"argsArray","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":":可選的。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個數組或者類數組對象,其中的數組元素將作爲單獨的參數傳給 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"attrs":{}},{"type":"text","text":" 函數。如果該參數的值爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":" 或  ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":",則表示不需要傳入任何參數。從ECMAScript 5 開始可以使用類數組對象。 瀏覽器兼容性 請參閱本文底部內容。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"返回值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調用有指定","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":"值和參數的函數的結果。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"call和apply的區別","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它們的作用一模一樣,區別僅在於傳入參數形式的不同。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5c/5c13fa338c1f57e0bbf2ec954cbd8c7e.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 接受兩個參數,第一個參數指定了函數體內 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 對象的指向,第二個參數爲一個帶下標的集合,這個集合可以爲數組,也可以爲類數組,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法把這個集合中的元素作爲參數傳遞給調用的函數:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const func = function (a, b, c) {\n console.log([a, b, c]);\n};\nfunc.apply(null, [1, 2, 3]); // [ 1, 2, 3 ]","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這段代碼中,參數 1、2、3 被放在數組中一起傳入 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"attrs":{}},{"type":"text","text":" 函數,它們分別對應 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"attrs":{}},{"type":"text","text":" 參數列表中的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"a 、b 、c","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 傳入的參數數量不固定,跟 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 相同的是,第一個參數也是代表函數體內的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 指向,從第二個參數開始往後,每個參數被依次傳入函數:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const func = function (a, b, c) {\n console.log([a, b, c]);\n};\nfunc.call(null, 1, 2, 3); // [ 1, 2, 3 ]","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調用一個函數時,Javascript 的解釋器並不會計較形參和實參在數量、類型以及順序上的區別,Javascript 的參數在內部就是用一個數組來表示的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從這個意義上說,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 比 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 的使用率更高,不關心具體多少參數被傳入函數,只要用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 一股腦地推過去就可以了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 是包裝在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 上的一顆語法糖,如果明確地知道函數接收多少個參數,而且想一目瞭然地表達形參和實參的對應關係,那麼可以用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 來傳送參數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 或者 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":" 的時候,如果傳入的第一個參數爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":" ,函數體內的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 會指向默認的宿主對象,在瀏覽器中則是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"window","attrs":{}}],"attrs":{}},{"type":"text","text":" :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"const func = function(a,b,c){\n console.log(this === window );\n}\nfunc.apply(null,[1,2,3]); // true","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果是在嚴格模式下,函數體內的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 還是爲 null :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const func = function (a, b, c) {\n console.log(this === window);\n};\nfunc.apply(null, [1, 2, 3]); // true","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"改變this指向","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const obj = {\n site: \"https://www.infoq.com\",\n};\nfunction func() {\n console.log(this.site);\n}\nfunc.call(obj); // https://www.infoq.com","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"call","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法的第一個參數是作爲函數上下文的對象,上面的代碼把 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"obj","attrs":{}}],"attrs":{}},{"type":"text","text":" 作爲參數傳給了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"attrs":{}},{"type":"text","text":",此時函數裏的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 便指向了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"obj","attrs":{}}],"attrs":{}},{"type":"text","text":" 對象。此處 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"func","attrs":{}}],"attrs":{}},{"type":"text","text":" 函數相當於下面的方法:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const obj = {\n site: \"https://www.infoq.com\",\n};\nfunction func() {\n console.log(obj.site);\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"apply()方法的一些妙用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Math.max","attrs":{}}],"marks":[{"type":"strong"}],"attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 可以實現得到數組中最大的一項","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Math.max","attrs":{}}],"attrs":{}},{"type":"text","text":"不支持","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Math.max([param1,param2])","attrs":{}}],"attrs":{}},{"type":"text","text":"也就是數組,但是它支持","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Math.max(param1,param2...)","attrs":{}}],"attrs":{}},{"type":"text","text":",所以可以根據","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":"的特點來解決 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"var max=Math.max.apply(null,array)","attrs":{}}],"attrs":{}},{"type":"text","text":",這樣就輕易的可以得到一個數組中的最大項(","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":"會將一個數組轉換爲一個參數接一個參數的方式傳遞給方法)這塊在調用的時候第一個參數給了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":",這是因爲沒有對象去調用這個方法,我只需要用這個方法幫我運算,得到返回的結果就行,所以直接傳遞了一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":"過去。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用這種方法也可以實現得到數組中的最小項:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Math.min.apply(null,array)","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Array.prototype.push","attrs":{}}],"attrs":{}},{"type":"text","text":"實現兩個數組的合併","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"push","attrs":{}}],"attrs":{}},{"type":"text","text":"方法沒有提供","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"push","attrs":{}}],"attrs":{}},{"type":"text","text":"一個數組,但是它提供了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"push(param1,param2...paramN)","attrs":{}}],"attrs":{}},{"type":"text","text":",同樣也可以用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apply","attrs":{}}],"attrs":{}},{"type":"text","text":"來轉換一下這個數組,即:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const target = new Array(\"1\", \"2\", \"3\");\nconst source = new Array(\"4\", \"5\", \"6\");\nconst result = Array.prototype.push.apply(target, source); // 得到合併後數組的長度,因爲push就是返回一個數組的長度\nconsole.log(target); // [ '1', '2', '3', '4', '5', '6' ]\nconsole.log(source); // [ '4', '5', '6' ]\nconsole.log(result); // 6","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章