Array.of vs" []" ;.何时使用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.of
Objective of this question is difference between Array.of
vs []
and not new Array
vs Array.of
推荐答案
Array.of()
和 Array()
/ <$之间存在一个细微差别c $ c> [] 构造函数。通常就像 Array()
,这个
Array.of()
将是 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.
最好记住它的定义;
注2:函数是一种有意的通用工厂方法;它
不要求它的值是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()
这里成为英雄;可能是最有用的数组方法之一。怎么会..?让我们看看......
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));
I还尝试制作 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 vs" []" ;.何时使用Array.of而不是“[]”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!