In the following demo only script, I clear (reset) the “tarr” two different ways.
My questions are:
1. Is one way better than the other? More acceptable? Or are they both the same?
2. Is there a concern with “memory leakage” using either method?
[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Clear Array Contents </title>
</head>
<body>
<button onclick=”initArr(arr1)”>Initial Array (1..5)</button>
<button onclick=”initArr(arr2)”>Initial Array (5..1)</button>
<button onclick=”initArr(arr1.concat(arr2))”>concat Arrays</button>
<p>
<!– <button onclick=”showArr(tarr)”>Show tarray</button> –>
<button onclick=”clearArr1(tarr)”>Clear []</button>
<button onclick=”clearArr2(tarr)”>Clear =0</button>
<p>
<div id=”debug”></div>
<script type=”text/javascript”>
var arr1 = [1,2,3,4,5];
var arr2 = [5,4,3,2,1];
var tarr = []; // global array
function initArr(arr) { tarr = arr.slice(0); showArr(tarr); }
function showArr(arr) { document.getElementById(‘debug’).innerHTML = arr; }
function clearArr1(arr) { tarr = []; showArr(tarr); }
function clearArr2(arr) { tarr.length = 0; showArr(tarr); }
</script>
</body>
</html>