JavaScript arguments物件全面介紹
1. 什麼是 arguments
MDN 上解釋:
arguments 是一個類別陣列物件。代表傳給一個function的參數清單。
我們先用一個範例直觀了解下 JavaScript 中的 arguments 長什麼樣子。
function printArgs() { console.log(arguments); } printArgs("A", "a", 0, { foo: "Hello, arguments" });
執行結果是:
["A", "a", 0, Object]
乍一看,結果是個數組,但並不是真正的數組,所以說arguments 是一個類數組的對象(想了解真正數組與類數組對象的區別可以一直翻到最後)。
再看看 arguments 表示的內容,其表示了函數執行時傳入函數的所有參數。在上面的例子中,代表了傳入 printArgs 函數中的四個參數,可以分別用 arguments[0]、 arguments[1]... 來取得單一的參數。
2. arguments 操作
2.1 arguments length
arguments 是個類別數組對象,其包含一個 length 屬性,可以用 arguments.length 來獲得傳入函數的參數個數。
function func() { console.log("The number of parameters is " + arguments.length); } func(); func(1, 2); func(1, 2, 3);
執行結果如下:
The number of parameters is 0 The number of parameters is 2 The number of parameters is 3
2.2 arguments 轉數組
通常使用下面的方法來將arguments 轉換成數組:
Array.prototype.slice.call(arguments);
還有一個更簡短的寫法:
rr空數組的slice 方法,而沒有從Array 的原型層面呼叫。 為什麼上面兩種方法可以轉換呢?首先,slice 方法得到的結果是一個數組,參數就是 arguments。事實上,滿足一定條件的物件都能被 slice 方法轉換成陣列。看個例子:[].slice.call(arguments);
const obj = { 0: "A", 1: "B", length: 2 }; const result = [].slice.call(obj); console.log(Array.isArray(result), result);
true ["A", "B"]
// Leaking arguments example1: function getArgs() { return arguments; } // Leaking arguments example2: function getArgs() { const args = [].slice.call(arguments); return args; } // Leaking arguments example3: function getArgs() { const args = arguments; return function() { return args; }; }
function getArgs() { const args = new Array(arguments.length); for(let i = 0; i < args.length; ++i) { args[i] = arguments[i]; } return args; }
function foo(a) { "use strict"; console.log(a, arguments[0]); a = 10; console.log(a, arguments[0]); arguments[0] = 20; console.log(a, arguments[0]); } foo(1);
1 1 10 1 10 20
function foo(a) { console.log(a, arguments[0]); a = 10; console.log(a, arguments[0]); arguments[0] = 20; console.log(a, arguments[0]); } foo(1);
1 1 10 10 20 20
function foo() { bar.apply(this, arguments); } function bar(a, b, c) { // logic }
function add(num1, num2) { console.log("Method one"); return num1 + num2; } function add(num1, num2, num3) { console.log("Method two"); return num1 + num2 + num3; } add(1, 2); add(1, 2, 3);
Method two Method two
function add(num1, num2, num3) { if (arguments.length === 2) { console.log("Result is " + (num1 + num2)); } else if (arguments.length === 3) { console.log("Result is " + (num1 + num2 + num3)); } } add(1, 2); add(1, 2, 3)
Result is 3 Result is 6
直接上栗子:
function func() { console.log(...arguments); } func(1, 2, 3);
執行結果是:
直接上栗子:1 2 3
function func(firstArg, ...restArgs) { console.log(Array.isArray(restArgs)); console.log(firstArg, restArgs); } func(1, 2, 3);
true 1 [2, 3]
function func(firstArg = 0, secondArg = 1) { console.log(arguments[0], arguments[1]); console.log(firstArg, secondArg); } func(99);
99 undefined 99 1
const obj = { 0: "a", 1: "b" }; const arr = [ "a", "b" ];
伪数组的特性就是长得像数组,包含一组数据以及拥有一个 length 属性,但是没有任何 Array 的方法。再具体的说,length 属性是个非负整数,上限是 JavaScript 中能精确表达的最大数字;另外,类数组对象的 length 值无法自动改变。
如何自己创建一个类数组对象?
function Foo() {} Foo.prototype = Object.create(Array.prototype); const foo = new Foo(); foo.push('A'); console.log(foo, foo.length); console.log("foo is an array? " + Array.isArray(foo));
执行结果是:
["A"] 1 foo is an array? false
也就是说 Foo 的示例拥有 Array 的所有方法,但类型不是 Array。
如果不需要 Array 的所有方法,只需要部分怎么办呢?
function Bar() {} Bar.prototype.push = Array.prototype.push; const bar = new Bar(); bar.push('A'); bar.push('B'); console.log(bar);
执行结果是:
Bar {0: "A", 1: "B", length: 2}
参考:
JavaScript中的数组与伪数组的区别
MDN arguments
Avoid modifying or passing arguments into other functions — it kills optimization
Optimization killers
Why isn't a function's arguments object an array in Javascript?
arguments 对象
Advanced Javascript: Objects, Arrays, and Array-Like objects
JavaScript 特殊对象 Array-Like Objects 详解
What is a good way create a Javascript array-like object?

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

本文探討了Java收藏框架的有效使用。 它強調根據數據結構,性能需求和線程安全選擇適當的收集(列表,設置,地圖,隊列)。 通過高效優化收集用法

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

本教程將介紹如何使用 Chart.js 創建餅圖、環形圖和氣泡圖。此前,我們已學習了 Chart.js 的四種圖表類型:折線圖和條形圖(教程二),以及雷達圖和極地區域圖(教程三)。 創建餅圖和環形圖 餅圖和環形圖非常適合展示某個整體被劃分為不同部分的比例。例如,可以使用餅圖展示野生動物園中雄獅、雌獅和幼獅的百分比,或不同候選人在選舉中獲得的投票百分比。 餅圖僅適用於比較單個參數或數據集。需要注意的是,餅圖無法繪製值為零的實體,因為餅圖中扇形的角度取決於數據點的數值大小。這意味著任何占比為零的實體

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。
