Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

javascript - 创建零填充JavaScript数组的最有效方法?(Most efficient way to create a zero filled JavaScript array?)

在JavaScript中创建任意长度的零填充数组的最有效方法是什么?

  ask by dil translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ES6 introduces Array.prototype.fill .

(ES6引入了Array.prototype.fill 。)

It can be used like this:

(可以这样使用:)

new Array(len).fill(0);

Not sure if it's fast, but I like it because it's short and self-describing.

(不知道它是否很快,但是我喜欢它,因为它简短而自描述。)

It's still not in IE ( check compatibility ), but there's a polyfill available .

(它仍然不在IE中( 检查兼容性 ),但是有一个polyfill可用 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...