Shallow copy pattern

function extend(parent, child) {

    var i;

    child = child || {};

    for (i in parent) {

        if (parent.hasOwnProperty(i)) {

            child[i] = parent[i];

        }

    }

    return child;

}

Deep copy pattern

function extendDeep(parent, child) {

    var i,

    toStr = Object.prototype.toString,

        astr = "[object Array]";

    child = child || {};

    for (i in parent) {

        if (parent.hasOwnProperty(i)) {

            if (typeof parent[i] === "object") {

                child[i] = (toStr.call(parent[i]) === astr) ? [] : {};

                extendDeep(parent[i], child[i]);

            } else {

                child[i] = parent[i];

            }

        }

    }

    return child;

}

var dad = {

    counts: [1, 2, 3],

    reads: {

        paper: true

    }

};

var kid = extendDeep(dad);

kid.counts.push(4);

kid.counts.toString(); // "1,2,3,4"

dad.counts.toString(); // "1,2,3"

(dad.reads === kid.reads).toString(); // false

kid.reads.paper = false;

kid.reads.web = true;

dad.reads.paper; // true

Firebug (Firefox extensions are written in JavaScript) has a method called extend()that makes shallow copies  and  jQuery’s  extend() creates  a  deep  copy.  YUI3  offers  a  method  called Y.clone(), which creates a deep copy and also copies over functions by binding them to the child object.

Advantage

There are no prototypes involved in this pattern at all; it’s only about objects and their own properties.

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 6.5 Inheritance by Copying Properties的更多相关文章

  1. JavaScript Patterns 6.4 Prototypal Inheritance

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

  2. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

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

  3. 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 ...

  4. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  5. JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

    Gets a length property containing the number of arguments the function expects: function func(a, b, ...

  6. 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 ...

  7. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  8. JavaScript Patterns 6.3 Klass

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

  9. JavaScript Patterns 5.7 Object Constants

    Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...

随机推荐

  1. android The connection to adb is down 错误信息

    之前运行eclipse执行android应用都没问题,今天突然出问题了,控制台报错: The connection to adb is down, and a severe error has occ ...

  2. Java总结篇系列:Java 反射

    Java反射: package com.corn; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...

  3. 泛函编程(4)-深入Scala函数类

    既然是泛函编程,多了解一下函数自然是免不了的了: 方法(Method)不等于函数(Function) 方法不是函数但可以转化成函数:可以手工转换或者由编译器(compiler)在适当的情况下自动转换. ...

  4. php中读写excel表格文件示例。

    测试环境:php5.6.24.这块没啥兼容问题. 需要更多栗子,请看PHPExcel的examples.还是蛮强大的. 读取excel文件. 第一步.下载开源的PHPExcel的类库文件,官方网站是h ...

  5. buffer和cache有什么本质区别

    在free命令展示机器的内存消耗情况,会像这样展示

  6. jQuery Countdown Timer 倒计时效果

    这个一款简单的 jQuery 倒计时插件,用于显示剩余的天数,小时,分钟和秒.倒计时功能是非常有用的一个小功能,可以告诉用户多久以后您的网站将会发布或者关闭进行维护,还可以用于举办活动的开始和停止的倒 ...

  7. Photopile JS – 帮助你实现精致的照片堆叠效果

    Photopile JS 是模拟照片散布堆叠在一起的 JavaScript/jQuery 图片库.点击缩略图,照片会弹出放大 ,再次点击照片会返回.缩略图是可拖动的,允许照片深深的堆在一起而不被覆盖, ...

  8. Papa Parse – 超强大的多线程 CSV 文本解析库

    Papa Parse 是一个与众不同的,在网页上运行的第一个多线程的 CSV 解析器.它可以解析千兆字节大小文件而不会导致浏览器崩溃.它能够正确地处理格式不正确或边缘的情况下的 CSV 文本.它可以分 ...

  9. 乱码之MyEclipse控制台

    今天突然发现控制台出现乱码,查了资料解决方案不一. 我的解决方案如下: Run -> Debug Configuration... -> MyEclipse Servler -> M ...

  10. Ubuntu 各版本代号简介

    起名字是件伤脑筋的事,但是程序猿们似乎最喜欢干伤脑筋的活.Android 的每个版本都有个甜点的别名,而 Ubuntu ,每个版本都有一个更为特色的名字,这个名字由一个形容词和一个动物名称组成,并且, ...