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.

  1. function mix() {
  2.  
  3. var arg, prop, child = {};
  4.  
  5. for (arg = 0; arg < arguments.length; arg += 1) {
  6.  
  7. for (prop in arguments[arg]) {
  8.  
  9. if (arguments[arg].hasOwnProperty(prop)) {
  10.  
  11. child[prop] = arguments[arg][prop];
  12.  
  13. }
  14.  
  15. }
  16.  
  17. }
  18.  
  19. return child;
  20.  
  21. }
  22.  
  23. var cake = mix({
  24.  
  25. eggs: 2,
  26.  
  27. large: true
  28.  
  29. }, {
  30.  
  31. butter: 1,
  32.  
  33. salted: true
  34.  
  35. }, {
  36.  
  37. flour: "3 cups"
  38.  
  39. }, {
  40.  
  41. sugar: "sure!"
  42.  
  43. });
  44.  
  45. 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. 组件Newtonsoft.Json实现object2json转换

    很多情况下,我们需要把数据类型做一些转换,供其它外部的子系统调用. 最为典型的是生成json格式供javascript作调用. 现成的组件Newtonsoft.Json可以实现object2json之 ...

  2. vs2012运行项目报未能加载文件或程序集“System.Web.Mvc, Version=4.0.0.1,Culture=neutral”问题和解决方法

    原先本地项目版本(4.0.0.1)高于服务器版本(4.0.0.0),本地项目改成服务器版本4.0.0.0时,发布后的项目报这个错误

  3. XE7 Update 1 选 iOS 8.1 SDK 发布 iPhone 3GS 实机测试

    测试实机:iPhone 3GS(v6.1.2)其它机种也可以正常发布,方法以此类推 开发环境:Delphi XE7 Update 1(选择 iOS 8.1 SDK) 发布时需要到 Project &g ...

  4. 实现在ios开发中的App滑动封面 UIScrollView

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _scrol ...

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

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

  6. python数据类型详解及列表字典集合推导式详解

    一.运算符 Python语言支持以下类型的运算符: 算术运算符 如: #!/usr/bin/env python # -*- coding:utf-8 -*- a = 5 b = 6 print(a ...

  7. JSON.stringify()、JSON.parse()和eval(string)

    1.JSON.stringify()用于从一个对象解析出字符串,eg: var obj = {"name":"奔跑的蜗牛","age":&q ...

  8. 从零开始,做一个NodeJS博客(四):服务器渲染页面与Pjax

    标签: NodeJS 0 一个星期没更新了 = = 一直在忙着重构代码,以及解决重构后出现的各种bug 现在CSS也有一点了,是时候把遇到的各种坑盘点一下了 1 听歌排行 API 修复与重构 1.1 ...

  9. Python 操作 MySQL 之 pysql 与 ORM(转载)

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  10. Android存储访问及目录

    Android存储访问及目录 Android的外部存储 Android支持外部存储(case-insensitive filesystem with immutable POSIX permissio ...