Is JavaScript's array.clear() not a function? [duplicate]

谁都会走 提交于 2019-12-02 19:55:46

Nope, it's not. But drawnDivs.length = 0 should work.

drawnDivs = [];

subhaze

It was answered in Stack Overflow question How do I empty an array in JavaScript?.

Two examples from the answer:

var A = ['some', 'values', 'here'];

//Method 1

//(This was my original answer to the question)

A = [];




// Method 2 (as suggested by Matthew Crumley)

A.length = 0

And here is a nice write up on these two methods by Dr. Axel Rauschmayer.

user3271659

An optimized way to do it is:

while (arr.pop()) {}

See http://jsperf.com/kbk-clear-array/2.

Debosmit Ray

You could alternately use the Prototype library and then, use Prototype's clear() method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!