zoukankan      html  css  js  c++  java
  • 继承之重写prototype

    function Ff(){}
    //undefined
    Ff.prototype={a:"ss"}
    //Object {a: "ss"}
    var f1= new Ff()
    //undefined
    Ff.prototype.constructor
    //Object() { [native code] }
    f1.constructor
    //Object() { [native code] }

    重写prototype,导致实例的constructor发生改变。

    var extend = function(protoProps, staticProps) {//{defaults:{name:,price}}
        var parent = this;
        var child;
    
        // The constructor function for the new subclass is either defined by you
        // (the "constructor" property in your `extend` definition), or defaulted
        // by us to simply call the parent's constructor.
        if (protoProps && _.has(protoProps, 'constructor')) {
          child = protoProps.constructor;
        } else {
          child = function(){ return parent.apply(this, arguments); };
        }
    
        // Add static properties to the constructor function, if supplied.
        _.extend(child, parent, staticProps);
    
        // Set the prototype chain to inherit from `parent`, without calling
        // `parent`'s constructor function.
        var Surrogate = function(){ this.constructor = child; };
        Surrogate.prototype = parent.prototype;
        child.prototype = new Surrogate;
    
        // Add prototype properties (instance properties) to the subclass,
        // if supplied.
        if (protoProps) _.extend(child.prototype, protoProps);
    
        // Set a convenience property in case the parent's prototype is needed
        // later.
        child.__super__ = parent.prototype;
    
        return child;
      };
  • 相关阅读:
    Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
    让Scrapy的Spider更通用
    API例子:用Python驱动Firefox采集网页数据
    API例子:用Java/JavaScript下载内容提取器
    Python即时网络爬虫:API说明
    Python: xml转json
    git 更新本地代码
    数据库事务
    Python的线程、进程和协程
    Java基础语法
  • 原文地址:https://www.cnblogs.com/darr/p/5004650.html
Copyright © 2011-2022 走看看