Reference and array clone
2009-06-30 11:39
323 查看
In JavaScript, object and array are passed by reference.
Sometimes you may come into trouble if you don’t keep this in mind, especial in
recursion.
Let’s see an example:
First, prepare a JavaScript array to present a tree data:
The expected html structure is:
Our JavaScript code:
Next, we want to store the tree node’s path.
For example, leaf1 - [root1, leaf1], leaf2 - [root1, leaf2], root2 - [root2]
We modifiy the resolveTree function:
The logic must be right, but the result is suprising.
No matter which node i click, the alert box contains an empty string.
What’s the matter?
oh… the array is passed by reference….
The variable path in the code - liNode.data(”path”, path) - is a reference. Then
we want a clone of the array.
There are many method to create a array clone.
The final solution:
Sometimes you may come into trouble if you don’t keep this in mind, especial in
recursion.
Let’s see an example:
First, prepare a JavaScript array to present a tree data:
var treeData = [{ "name": "root1", "kids": [{ "name": "leaf1" }, { "name": "leaf2"}] }, { "name": "root2"}];
The expected html structure is:
<ul> <li>root1 <ul> <li>leaf1</li> <li>leaf2</li> </ul> </li> <li>root2</li> </ul>
Our JavaScript code:
function resolveTree(rootNode, kids) { var ulNode = $("<ul />").appendTo(rootNode); $.each(kids, function(i, item) { var liNode = $("<li />").html(item.name).appendTo(ulNode); if (item.kids) { resolveTree(liNode, item.kids); } }); } resolveTree($("#tree"), treeData);
Next, we want to store the tree node’s path.
For example, leaf1 - [root1, leaf1], leaf2 - [root1, leaf2], root2 - [root2]
We modifiy the resolveTree function:
function resolveTree(rootNode, kids, path) { var ulNode = $("<ul />").appendTo(rootNode); $.each(kids, function(i, item) { var liNode = $("<li />").html(item.name).appendTo(ulNode); path.push(item.name); liNode.data("path", path).click(function(event) { event.stopPropagation(); alert($(this).data("path")); }); if (item.kids) { resolveTree(liNode, item.kids, path); } path.pop(); }); } resolveTree($("#tree"), treeData, []);
The logic must be right, but the result is suprising.
No matter which node i click, the alert box contains an empty string.
What’s the matter?
oh… the array is passed by reference….
The variable path in the code - liNode.data(”path”, path) - is a reference. Then
we want a clone of the array.
There are many method to create a array clone.
// Method 1 var newArray = []; for (var i = 0; i < oldArray.length; i++) { newArray.push(oldArray[i]); } // Method 2 var newArray = oldArray.slice(0); // Method 3 var newArray = [].concat(oldArray);
The final solution:
$(function() { var treeData = [{ "name": "root1", "kids": [{ "name": "leaf1" }, { "name": "leaf2"}] }, { "name": "root2"}]; function resolveTree(rootNode, kids, path) { var ulNode = $("<ul />").appendTo(rootNode); $.each(kids, function(i, item) { var liNode = $("<li />").html(item.name).appendTo(ulNode); path.push(item.name); liNode.data("path", [].concat(path)).click(function(event) { event.stopPropagation(); alert($(this).data("path")); }); if (item.kids) { resolveTree(liNode, item.kids, path); } path.pop(); }); } resolveTree($("#tree"), treeData, []); });
相关文章推荐
- Reference and array clone
- Reference To Array And Pointer To Array
- PHP中对象的clone和引用的区别(Object Cloning and Passing by Reference in PHP)
- What is the difference between Array.Clone and Array.Copy Method
- Kotlin Reference (三) Basic Types, String, Array and Imports
- Reference to array and hash
- array,pointer and reference
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- AFNetworking 报错 JSON text did not start with array or object and option to allow fragments not set
- CodeforcesRound #136 (Div. 2) D. Little Elephant andArray
- find element in both array and vector using one myfind function
- ruby merge array of hashes based on the key and order it by number of key/value pair
- Git Reference---Getting and Creating Projects
- Merge array and hash in ruby if key appears in array
- CF 220B Little Elephant and Array
- "Contains Duplicate II" and "Merge Sorted Array"
- Divide and Conquer-215-Kth Largest Element in an Array
- Lintcode373 Partition Array by Odd and Even solution 题解
- CF 558B(Amr and The Large Array-计数)
- codeforces #312 B B. Amr and The Large Array(统计)