2017-01-03 81 views
0

外部合作伙伴爲我們開發了一個jQuery插件。 我想重置一些設置,併爲我的驚喜,它不工作。Jquery擴展深層拷貝問題

我做了一些調試和來到了以下結果:

我注意到,他們用$ .extend

var t = '{"config":{"filters":{"filtersPerCarousel":2,"sliders":[{"machineName":"brightness","filter":"brightness","range":100,"step":5,"value":0,"translationKey":"filter.slider.brightness"},{"machineName":"contrast","filter":"contrast","range":100,"step":5,"value":0,"translationKey":"filter.slider.contrast"}],"presets":[{"machineName":"original","filters":[],"previewImage":"/images/filters/original.png","translationKey":"filter.original"},{"machineName":"blackwhite","filters":[{"filter":"saturation","settings":[-100]}],"previewImage":"/images/filters/blackwhite.png","translationKey":"filter.blackwhite"},{"machineName":"sepia","filters":[{"filter":"greyscale","settings":[]},{"filter":"sepia","settings":[60]}],"previewImage":"/images/filters/sepia.png","translationKey":"filter.sepia"}]}}}'; 
 
var o = '{"config":{"filters":{"sliders":[],"presets":[]}}}'; 
 
settings = $.extend(true, {}, jQuery.parseJSON(t), jQuery.parseJSON(o)); 
 
console.log(settings.config);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

深當副本isset的深層副本爲true我的config.sliders沒有被修改並保持原始狀態。

當我刪除了深拷貝,只是使用的配置被修改$ .extend

var t = '{"config":{"filters":{"filtersPerCarousel":2,"sliders":[{"machineName":"brightness","filter":"brightness","range":100,"step":5,"value":0,"translationKey":"filter.slider.brightness"},{"machineName":"contrast","filter":"contrast","range":100,"step":5,"value":0,"translationKey":"filter.slider.contrast"}],"presets":[{"machineName":"original","filters":[],"previewImage":"/images/filters/original.png","translationKey":"filter.original"},{"machineName":"blackwhite","filters":[{"filter":"saturation","settings":[-100]}],"previewImage":"/images/filters/blackwhite.png","translationKey":"filter.blackwhite"},{"machineName":"sepia","filters":[{"filter":"greyscale","settings":[]},{"filter":"sepia","settings":[60]}],"previewImage":"/images/filters/sepia.png","translationKey":"filter.sepia"}]}}}'; 
 
var o = '{"config":{"filters":{"sliders":[],"presets":[]}}}'; 
 
settings = $.extend({}, jQuery.parseJSON(t), jQuery.parseJSON(o)); 
 
console.log(settings);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

爲什麼我的對象修改與深拷貝未設置爲什麼不是用深拷貝修改? 這是更有意義的,這是另一種方式?

jquery的版本是1.9.1,但我測試了這個多版本,他們都給出了相同的結果。

+0

你必須設置深複製到TRUE;深拷貝;那是每個設計。你在這裏擔心什麼?什麼不起作用? – Developer

回答

0

請看看here

jQuery擴展MERGES傳遞給它的所有對象,它不覆蓋對象。如果你傳遞一個填充對象和一個空的對象,它將合併並獲取來自兩者的所有數據。要獲取所有內部數據,您需要將深度複製設置爲true。

如果您需要重置的一些價值觀,你可以手動設置

var t = '{"config":{"filters":{"filtersPerCarousel":2,"sliders":[{"machineName":"brightness","filter":"brightness","range":100,"step":5,"value":0,"translationKey":"filter.slider.brightness"},{"machineName":"contrast","filter":"contrast","range":100,"step":5,"value":0,"translationKey":"filter.slider.contrast"}],"presets":[{"machineName":"original","filters":[],"previewImage":"/images/filters/original.png","translationKey":"filter.original"},{"machineName":"blackwhite","filters":[{"filter":"saturation","settings":[-100]}],"previewImage":"/images/filters/blackwhite.png","translationKey":"filter.blackwhite"},{"machineName":"sepia","filters":[{"filter":"greyscale","settings":[]},{"filter":"sepia","settings":[60]}],"previewImage":"/images/filters/sepia.png","translationKey":"filter.sepia"}]}}}'; 
 

 
settings = jQuery.parseJSON(t); 
 

 
settings.config.filters.sliders = []; 
 
settings.config.filters.presets = []; 
 

 
console.log(settings);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>