How to copy/clone a hash/object in JQuery?

前端 未结 6 2163
既然无缘
既然无缘 2021-02-05 00:52

I have a simple object (or hash) in Javascript:

var settings = {
  link: \'http://example.com\',
  photo: \'http://photos.com/me.jpg\'
};

I nee

6条回答
  •  佛祖请我去吃肉
    2021-02-05 01:44

    var clone = $.extend(true, {}, settings);
    

    Set first argument to true.

    EDIT: First argument true signifies deep copy. For given example in original question there is no need for deep copy since there are simple immutable key-value pairs. For question in title - as a general case - use deep copy. Otherwise you get half-copy.

提交回复
热议问题