首页 > 网站 > WEB开发 > 正文

JavaScript Patterns 6.6 Mix-ins

2024-04-27 14:21:17
字体:
来源:转载
供稿:网友

javaScript Patterns 6.6 Mix-ins

2014-07-21 10:58 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑

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)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表