ITPub博客

首页 > IT职业 > IT图书 > Reference and array clone

Reference and array clone

IT图书 作者:liunh 时间:2021-09-09 09:41:49 0 删除 编辑

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:

    var treeData = [{ "name": "root1",        "kids": [{ "name": "leaf1" }, { "name": "leaf2"}] },        { "name": "root2"}];

The expected html structure is:

    
            
  • root1            
                      
    • leaf1
    •                 
    • leaf2
    •             
            
  •         
  • root2
  •     

Our JavaScript code:

    function resolveTree(rootNode, kids) {        var ulNode = $("
    ").appendTo(rootNode);        $.each(kids, function(i, item) {            var liNode = $("
  • ").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 = $("
      ").appendTo(rootNode);        $.each(kids, function(i, item) {            var liNode = $("
    • ").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 

      The final solution:

          $(function() {         var treeData = [{ "name": "root1", "kids": [{ "name": "leaf1" }, { "name": "leaf2"}] }, { "name": "root2"}];         function resolveTree(rootNode, kids, path) {            var ulNode = $("
        ").appendTo(rootNode);            $.each(kids, function(i, item) {                var liNode = $("
      • ").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, []);    });
      • 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2790/viewspace-2806199/ ,如需转载,请注明出处,否则将追究法律责任。

        请登录后发表评论 登录
        全部评论

        注册时间:2019-08-16

        • 博文量
          111
        • 访问量
          969