zoukankan      html  css  js  c++  java
  • js reuse code object copy

    /*Prototypal Inheritance*/
    function object(o) {
    function F() { }
    F.prototype = o;
    return new F();
    }

    // object to inherit from
    var parent = {
    name: "Papa"
    };
    // the new object
    var child = object(parent);
    // testing
    alert(child.name); // "Papa"

    //used in function
    function Person() {
    // an "own" property
    this.name = "Adam";
    }
    // a property added to the prototype
    Person.prototype.getName = function () {
    return this.name;
    };
    // create a new person
    var papa = new Person();
    // inherit
    var kid = object(papa);
    // test that both the own property
    //
    and the prototype property were inherited
    kid.getName(); // "Adam"

    /*shallow copy*/
    function extend(parent, child) {
    var i;
    child = child || {};
    for (i in parent) {
    if (parent.hasOwnProperty(i)) {
    child[i] = parent[i];
    }
    }
    }

    /*Deep Copy*/
    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;
    }
  • 相关阅读:
    oracle的over函数应用(转载)
    Oracle decode()函数应用
    EL表达式显示数据取整问题
    null值与空值比较
    case when语句的应用
    堆排序
    希尔排序
    插入排序
    异或运算
    选择排序
  • 原文地址:https://www.cnblogs.com/RitaRichard/p/2291356.html
Copyright © 2011-2022 走看看