简体   繁体   English

我如何才能停止通过引用?

[英]how can i stop pass by reference?

i have a problem with passing by reference or value. 我有一个通过引用或值传递的问题。

and i know people might thing this is a duplicate post but i did not find a way to solve this. 而且我知道人们可能会认为这是重复的帖子,但是我没有找到解决此问题的方法。

I have something like: 我有类似的东西:

//<div data-content="{bigObject}" id="SomeId"></div>

   // Originally bigObject was something like:

bigObject = {
    place : 'home',
    time : ''
};

$('#SomeId').click(function(){
    $(this).someFunction();
});

(function($){
    $.fn.someFunction = function(){
        var m = {
            obj : this,
            bigObject : '',
            initialize : function(){
                m.bigObject = m.obj.data('content');
                m.manipulateObject();
            },
            manipulateObject : function(){
                m.bigObject.place = 'work';
            }
        }
        m.initialize();
        return m.obj;
    }
})(jQuery);

The issue is when i click on SomeId the big object changes both in the div's data('content') and in the function m.bigObject.place 问题是,当我单击SomeId ,大对象在div的data('content')和函数m.bigObject.place中都发生了m.bigObject.place

Is there a way to not change them both ? 有没有办法不改变它们两者?

You can create a new copy of the object and use that. 您可以创建该对象的新副本并使用它。 You can use jquery.extend for that. 您可以使用jquery.extend

var newObject = $.extend({}, m.bigObject);

Instead of doing 而不是做

manipulateObject : function(){
    m.bigObject.place = 'work';
}

You can do: 你可以做:

manipulateObject : function(object){
    object.place = 'work';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM