• 原型式继承


    function object(o){

    function F(){}
    F.prototype = o;
    return new F();
    }

    //借助原型可以基于已有的对象创建新的对象,同时还不必因此chu创建自定义类型,

    //在object函数的内部,先创建了一个临时性的构造函数,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的
    //的新实例。从本质上讲,object()对传入其中的对象执行了一次前复制

    var person = {
    name:"Nicholas",
    friends:["shelby","count","wan"]
    };

    var anotherPerson = object(person);
    anotherPerson.name = "Greg";
    anotherPerson.friends.push("Rob");

    var anotherPerson2 = object(person);
    anotherPerson2.name = "Greg2";
    anotherPerson2.friends.push("Rob2");

    console.log(anotherPerson2.friends);

    //为了规范化原型继承 Object.create();

    //在传入一个参数的情况下与object函数一样

    var person = {
    name:"shalio",
    friends:["hahs","jk","ll"]
    };

    var anotherPerson=Object.create(person);

    //也可以为此方法传入两个个参数 与Object.defineProperties相似

    var newPerson = Object.create(person,{name:{value:"hhhh"}});

  • 相关阅读:
    Notepadd ++ PluginManager安装
    Srping cloud Ribbon 自定义负载均衡
    Spring cloud Eureka 和 Zookeeper 比较
    Spring cloud info信息显示
    kafka 在Windows端安装 找不到或无法加载主类 的解决方案
    Linux kafka 单机安装
    mina
    @bzoj
    @51nod
    @topcoder
  • 原文地址:https://www.cnblogs.com/lovefan/p/3884202.html
Copyright © 2020-2023  润新知