本文介绍了是否有正确的方法来$。扩展jQuery中的嵌套属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我拥有什么,我需要什么。这很容易。What I have and what I need. It's easy.默认选项(有嵌套属性):The default options (there are nested properties):{ sDom: 'frt<"tfoot"lp>', bInfo: false, sPaginationType: "full_numbers", oLanguage: { sSearch: "", sLengthMenu: "Show _MENU_", oPaginate: { sFirst: "|<<", sLast: ">>|", sNext: ">>", sPrevious: "<<" } }}实际期权:{ oLanguage: { oPaginate: { sNext: "MODIFIED" } }} $ .extend的结果:The result of $.extend:{ sDom: 'frt<"tfoot"lp>', bInfo: false, sPaginationType: "full_numbers", oLanguage: { oPaginate: { sNext: "MODIFIED" } }}我需要的是使用实际选项正确扩展默认选项并获得以下结果(一个属性已被修改):What I need is to properly extend the defaults options with the actual options and get the following result (one property has been modified):{ sDom: 'frt<"tfoot"lp>', bInfo: false, sPaginationType: "full_numbers", oLanguage: { sSearch: "", sLengthMenu: "Show _MENU_", oPaginate: { sFirst: "|<<", sLast: ">>|", sNext: "MODIFIED" sPrevious: "<<" } }}问题是$。 extend函数忽略嵌套属性并仅操作第一级属性。现在我手动$ .extend每个嵌套属性,但我想这不是解决方案。The problem is that $.extend function ignores nested properties and operates only first-level properties. Now I have manually $.extend each of the nested properties, but I guess it is not a solution.推荐答案你需要递归副本,将 true 作为第一个参数传递:You need a recursive copy by passing true as the first parameter:var defaults = {...}var actual = {...}//recursively merge to a blank objectconsole.log($.extend(true,{}, defaults, actual)) 这篇关于是否有正确的方法来$。扩展jQuery中的嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 03:29