JavaScript Patterns 6.6 Mix-ins
2014-07-21 10:58 小郝(Kaibo Hao) 阅读(486) 评论(0) 编辑 收藏 举报Loop through arguments and copy every property of every object passed to the function. And the result will be a new object that has the properties of all the source objects.
function mix() { var arg, prop, child = {}; for (arg = 0; arg < arguments.length; arg += 1) { for (prop in arguments[arg]) { if (arguments[arg].hasOwnProperty(prop)) { child[prop] = arguments[arg][prop]; } } } return child; } var cake = mix({ eggs: 2, large: true }, { butter: 1, salted: true }, { flour: "3 cups" }, { sugar: "sure!" }); console.dir(cake);
References:
JavaScript Patterns - by Stoyan Stefanov (O`Reilly)
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。