Array.of 与“[]".何时在“[]"上使用 Array.of? [英] Array.of vs "[ ]". When to use Array.of over "[ ]"?
问题描述
当我发现 Array.of.
根据 MDN,
Array.of() 方法使用可变数量的参数创建一个新的 Array 实例,而不管参数的数量或类型如何.
The Array.of() method creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
var a = Array.of(1,2,3,4,5);
console.log(a)
但是如果我已经知道这些值,我也可以将它们包装在 []
中以获得相同的输出.那么当我们可以/应该使用 Array.of
时,是否有特定的场景?与 []
相比,使用它有什么好处吗?
But if I already know the values, I can also wrap them in []
to get same output. So is there a specific scenario when we can/should use Array.of
? Also is there any benefit of using it over []
?
这个问题的目标是Array.of
vs []
和new Array
vs Array 之间的区别.
Objective of this question is difference between Array.of
vs []
and not new Array
vs Array.of
推荐答案
Array.of()
和 Array()
/ 之间有一个细微的区别[]
构造函数.通常就像 Array()
一样,Array.of()
中的 this
将是 Array
对象,它将使用 Array.constructor
即 function Array()
来构造它的结果.
There is one subtle difference between Array.of()
and Array()
/ []
constructor. Normally just like Array()
, the this
in Array.of()
will be the Array
object and it will use the Array.constructor
which is function Array()
to construct it's result.
然而 Array.of
可以通过改变它的绑定上下文来表现不同.如果绑定上下文可以用作构造函数(如果绑定对象是函数),它将使用该函数来构造.因此,让我们将 Array.of()
绑定到一个函数,看看会发生什么.
However Array.of
can behave differently by changing it's bound context. If the bound context can be used as a constructor (if the bound object is a function) it will use that function to construct. So let's bind the Array.of()
to a function and see what happens.
function Test(n){console.log(n)}
Test.prototype.last = function(){return this[this.length-1]};
var what = Array.of.call(Test, [5,6,7], {a:0,b:1}, 42, null, "this is last");
console.log(JSON.stringify(what,null,2));
console.log(what.last());
所以我们得到了一个类似thingy的数组,可以访问我们构造函数原型中的所有函数方法加上那些方法.
So we got an array like thingy with access to all function methods plus the ones at our constructor's prototype.
最好记住它是定义;
NOTE 2 of 函数是有意的泛型工厂方法;它不要求它的 this 值是 Array 构造函数.因此它可以被其他构造函数转移或继承可以使用单个数字参数调用.
NOTE 2 The of function is an intentionally generic factory method; it does not require that its this value be the Array constructor. Therefore it can be transferred to or inherited by other constructors that may be called with a single numeric argument.
好的,这对于数组子类化非常方便.我知道通过涉及 Object.setPrototypeOf()
或 __proto__
可以实现数组子类化,但它们有点不鼓励的操作,我们仍然可以在 Array.of() 的帮助下做类似的工作代码>.所以 .. 曾经被认为是无用的
Array.of()
在这里变成了英雄;可能是最有用的 Array 方法之一.怎么来的..?让我们看看...
OK this can be very handy for array sub-classing. I know array sub-classing is possible by involving Object.setPrototypeOf()
or __proto__
but they are somewhat discouraged operations and we can still do a similar job with the help of Array.of()
. So .. once known to be useless Array.of()
here becomes a hero; may be one of the most useful Array methods. How come..? let's see...
function SubArr(){}
SubArr.prototype = Object.create(Array.prototype); // make SubArr.prototype's prototype Array.prototype
SubArr.prototype.last = function(){return this[this.length-1]}; // add prototype methods to SubArr
var what = Array.of.call(SubArr, 1, 2, 3, 4, "this is last");
console.log(JSON.stringify(what,null,2));
console.log(what.last());
console.log(what.map(e => e));
console.log(what instanceof Array);
console.log(Array.isArray(what));
console.log(Object.prototype.toString.call(what));
我也尝试过使 SubArr.prototype.constructor = Array;
但 Array.isArray(what)
仍然导致 false
.
I have also tried making SubArr.prototype.constructor = Array;
but Array.isArray(what)
still resulted false
though.
这篇关于Array.of 与“[]".何时在“[]"上使用 Array.of?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!