目标
我想创建一个具有几个这样的参数的函数:
MyObject.calculateResponsiveHeights({
containers: {
".content",
".sidebar"
},
spacing: {
125, // This will be attributed to ".content"
240 // This will be attributed to ".sidebar"
}
});
问题
我不知道如何才能做到这一点。
我现在拥有的(功能实现——只是为了了解情况)
function calculateResponsiveMeasures() {
var containerContentResponsiveHeight = $(window).height() - 185,
sidebarProductsSummaryResponsiveHeight = $(window).height() - 255;
containerContentResponsiveHeight = containerContentResponsiveHeight + "px";
sidebarProductsSummaryResponsiveHeight = sidebarProductsSummaryResponsiveHeight + "px";
$(".container-content").css("height", containerContentResponsiveHeight);
$(".products-summary").css("height", sidebarProductsSummaryResponsiveHeight);
}
是啊,这很恶心吧?
观察结果
我并不是要求改进我的代码,也不是说这是更好还是更差的方法 - 我只是想更好地组织我的函数。
干杯!
最佳答案
关于带有几个参数的 JavaScript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16970058/