Three methods in the JavaScript array js empty empty empty array is an array of three ways js method

Empty arrays in JavaScript are three ways

js empty array method

1.splice function

arrayObject.splice(index,howmany,element1,.....,elementX)

index: Required provisions add / remove elements from where.

howmany: mandatory provisions of how many elements should be removed. This parameter is not specified, then remove all elements from index to the end of the original array.

element1: optional provisions to be added to a new element of the array.

<script type ="text/javascript">  
  var arr = [1,2,3,4];  
   arr.splice(0,arr.length);  
</script>  

2. assigned to the array length is 0

This method retains the properties of the other array.

Imparting a length less than the length of the array itself, the latter elements in the array will be truncated.

Given array length greater than itself, extend the length of the array, the element number is undefined.

<script type ="text/javascript">  
  var arr = [1,2,3,4];  
   arr.length = 0;
</script> 

Currently an array of Prototype  the Clear  arrays and mootools library  empty  this way to empty array.

 

Other languages ​​such as Java, length of its array is read-only, can not be assigned. Such as

int[] ary = {1,2,3,4};
ary.length = 0;

Java will be an error, the compiler pass.

3. Direct gives new array []

This way the arr reassigned to an empty array, if not before the array is referenced, will wait for garbage collection.

<script type ="text/javascript">  
  var arr = [1,2,3,4];  
   arr = [];
</script> 

Ext library Ext.CompositeElementLite like  clear  this way empty.

 

Guess you like

Origin www.cnblogs.com/yangai/p/11304193.html