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. C#写爬虫,版本V1.0

    之前看了Sql Server中的基本数据类型,发现image这个类型还是比较特殊的. 于是乎就做了一个将图片以二进制流形式存储的程序http://www.cnblogs.com/JsonZhangAA ...

  2. C# 通过GPS坐标,计算两点之间距离

    之前在网上有很多这种计算的,但是代码都不怎么全.经过多方打听查询.找到完整代码.现将代码共享给大家. 有需要者觉得有用者欢迎使用.觉得用或简单的高手,请绕. public static double ...

  3. springmvc(4)注解简单了解

    对于我这样的新人来说,因为是刚开始做项目,所以以前的技术不是用的很多,就比如springmvc来说,实际上使用的都是注解形式的,对于那些全部都是配置的来说,虽然也了解一些,但是实际上还是没试用过的. ...

  4. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  5. 【转】ubuntu源码编译安装php常见错误解决办法

    ./configure -prefix=/usr/local/php -with-config-file-path=/etc -with-mysql=mysqlnd -with-mysqli=mysq ...

  6. Fiddler (四) 实现手机的抓包

      Fiddler是我最喜爱的工具,几乎每天都用, 我已经用了8年了. 至今我也只学会其中大概50%的功能. Fiddler绝对称得上是"神器", 任何一个搞IT的人都得着的. 小 ...

  7. tomcat 5.5、6、7各版本的web-app标准

    刚把上周说的公司内各工程的web.xml给统一了,顺便看了下每个tomcat版本对应的标准servlet版本,如下: The XSD version is preferred since JSP 2. ...

  8. css知多少(10)——display

    1. 引言 网页的所有元素,除了“块”就是“流”,而且“流”都是包含在“块”里面的(最外层的body就是一个“块”).在本系列一开始讲<浏览器默认样式>的时候,大家也都看到了浏览器默认样式 ...

  9. 如何实现桌面App图标可以动态显示消息数(类似手机上的QQ图标)?

    手机上的APP , 像QQ和微信等都可以在图标上动态显示消息数(最大99) , 那么你有没有想过这些效果是如何实现的?桌面上开发的传统应用程序能否也实现类似的功能? 1 思路 桌面快捷方式的图标本质上 ...

  10. sublime text 之添加插件 并使用

    通过package  controll  install  安装各种插件: 1:AdvancedNewFile,安装完毕后 快捷键:ctr+alt+n:输入框中输入你要创建的文件夹和文件,就创建好了. ...