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,但我測試了這個多版本,他們都給出了相同的結果。
你必須設置深複製到TRUE;深拷貝;那是每個設計。你在這裏擔心什麼?什麼不起作用? – Developer