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)

JavaScript Patterns 6.6 Mix-ins的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  3. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  4. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  5. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  6. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  7. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  8. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. Oracle忘记密码的处理办法

    可能有的人在很长一段时间不用Oracle数据库,然后在反过来用的时候就会发现自己依然忘记了密码... 这里提供一个忘记密码后的解决办法 首先呢,你打开数据库用 sys或者是sysdba超级管理员用户名 ...

  2. font-family常见字体

    font-family:"Times New Roman",Georgia,Serif font-family:Arial,Verdana,Sans-serif font-fami ...

  3. Ajax 文件上传

    原文地址:http://blog.sina.com.cn/s/blog_5d64f7e3010127ns.html 用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest ...

  4. mysql乱码解决

    在 /etc/my.cnf加上下面二句: skip-character-set-client-handshakecharacter-set-server = utf8

  5. mysql安装中出现的问题,

    花了一天的时间明天mysql的安装方法: 自己的错误: 主要原因: (1):bin文件坏境配置出现了问题,没有重新在系统中配置文件 解决方法:右击电脑——属性——高级系统设置——变量配置——在path ...

  6. Java.io.DataInputStream.readInt()

    1. 代码 import java.io.DataInputStream; import java.io.IOException; public class Test { public static ...

  7. 修改windows系统文件权限

    修改windows系统文件总是提示没有权限,虽然已是administrator也不管用. 以下方法可以解决: 右键属性,安全,高级,所有者,编辑,选择当前用户并确定, 回到上一页再确定, 然后在安全页 ...

  8. Struts2框架的自定义类型转换器

    前言:对于java的基本数据类型及一些系统类(如Date类.集合类),Struts2提供了内置类型转换功能,但是也有一定的限制.所以就演示出自定义类型转换器 一.应用于局部类型转换器 eg.用户登录出 ...

  9. 常用的css命名规则:

    关于团队合作的css命名规范 常用的css命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制 ...

  10. easyUI 后台经典框架DEMO下载

    采用easyui 1.2.6 + jquery 1.7.2 设计 有不明白的朋友加群或加我QQ (709047174) Jquery-EasyUi-demo点击我下载