`
mismo
  • 浏览: 4814 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Comment of Dean Edwards’s Base Library By John Resig

阅读更多
Download from "http://www.apress.com/book/downloadfile/3291"

// The Base object constructor
var Base = function(obj){
    // Only work if there's something to extend
    if (obj) 
        // If used as Base(), use the prototype of the caller
        if (this == window) 
            Base.prototype.extend.call(obj, arguments.callee.prototype);
        
        // Otherwise, this is called as new Base(), so extend the object
        else 
            this.extend(obj);
};

Base.version = "1.0.2";

Base.prototype = {
    // A function for overriding one object property with another
    extend: function(source, value){
        var extend = Base.prototype.extend;
        
        // Check to see if we're overriding a property with a new value
        if (arguments.length == 2) {
            // Remember the original parent property value
            var ancestor = this[source];
            
            // Check to see if we're overriding a parent method, and that this.base()
            // is actually used by the overrider
            if ((ancestor instanceof Function) && (value instanceof Function) &&
            ancestor.valueOf() != value.valueOf() &&
            /\bbase\b/.test(value)) {
            
                // Remember the old function
                var method = value;
                
                // and build a new function wrapper, to have sane 
                value = function(){
                    // Remember the old value of this.base to be restored later
                    var previous = this.base;
                    // Calling this.base() calls the old parent function
                    this.base = ancestor;
                    
                    // Execute the new, overriding, function
                    var returnValue = method.apply(this, arguments);
                    
                    // Restore the this.base() property
                    this.base = previous;
                    
                    // Return the accurate return value
                    return returnValue;
                };
                
                // valueOf and toString get messed by our modified
                // wrapper function, so make them appear normal
                value.valueOf = function(){
                    return method;
                };
                value.toString = function(){
                    return String(method);
                };
            }
            
            // Attach the new property to the source
            return this[source] = value;
            
            // If only a source was provided, copy all properties over from
            // the parent class to this new child
        }
        else 
            if (source) {
                var _prototype = {
                    toSource: null
                };
                
                // We modify these two functions later on, so protect them
                var _protected = ["toString", "valueOf"];
                
                // if we are prototyping then include the constructor
                if (Base._prototyping) 
                    _protected[2] = "constructor";
                
                // Copy over the protected functions indvidually
                for (var i = 0; (name = _protected[i]); i++) 
                    if (source[name] != _prototype[name]) 
                        extend.call(this, name, source[name]);
                
                // Copy each of the source object's properties to this object
                for (var name in source) 
                    if (!_prototype[name]) 
                        extend.call(this, name, source[name]);
            }
        
        return this;
    },
    
    // The this.base() function which we'll be implementating later on
    base: function(){
    }
};

// A wrapper function for creating a new object constructor
Base.extend = function(_instance, _static){
    // Remember the extend function
    var extend = Base.prototype.extend;
    
    // Lets us do Base.extend() and get a blank object constructor
    if (!_instance) 
        _instance = {};
    
    // Make sure to include the constructor later
    Base._prototyping = true;
    
    // Build the prototype
    var _prototype = new this;
    extend.call(_prototype, _instance);
    
    // Build the constructor
    var constructor = _prototype.constructor;
    _prototype.constructor = this;
    
    delete Base._prototyping;
    
    // Create the wrapper for the constructor function
    var klass = function(){
        if (!Base._prototyping) 
            constructor.apply(this, arguments);
        this.constructor = klass;
    };
    
    // Which inherits from Base
    klass.prototype = _prototype;
    
    // Add all the extra Base methods
    klass.extend = this.extend;
    klass.implement = this.implement;
    klass.toString = function(){
        return String(constructor);
    };
    
    // Add in all the extra properties provided by the user
    extend.call(klass, _static);
    
    // Check for a single instance case
    var object = constructor ? klass : _prototype;
    
    // Class initialisation
    if (object.init instanceof Function) 
        object.init();
    
    // Return the new object constructor
    return object;
};

// A simple function that can be used to pull additional object properties
// into a constructor – effectively creating multiple inheritance
Base.implement = function(_interface){
    // If a constructor was provided, instead of a prototype,
    // get the prototype instead
    if (_interface instanceof Function) 
        _interface = _interface.prototype;
    
    // Extend the object with the methods from the parent object
    this.prototype.extend(_interface);
};


分享到:
评论

相关推荐

    ie7-js:Dean Edwards ie7-js 项目的一个分支,该项目曾经在 Google Code 上。 使 MSIE 表现得像符合标准的浏览器的 JavaScript 库

    关于IE7.js 是一个 JavaScript 库,用于使 Microsoft Internet Explorer 表现得像符合标准的浏览器。...地位当前版本:2.1 beta4。用法IE7.js 升级 MSIE5.5-6 以兼容 MSIE7。 <!--[if lt IE 7]>[removed][removed]&...

    grunt-packitjs:PackitJS 一个 grunt 插件,利用 Dean Edwards 的 Packer 有效地打包 javaScript 文件

    grunt-packitjs 这是一个 grunt 插件,它利用 Dean Edwards 的有效地打包 javaScript 文件。安装如果您使用 : npm install grunt-packitjs如果您不使用 npm,请克隆此存储库或使用 GitHub 存储库下载链接下载最新...

    Langes Handbook of Chemistry (John A. Dean)

    《兰格化学手册》(Langes Handbook of Chemistry) 是一本极具权威性的化学参考书,由John A. Dean编著。这本书在化学领域中被广泛使用,是科学家、工程师、学生和教师的重要工具书。它提供了丰富的化学数据、公式、...

    Jeff-Dean-s-Lecture-for-YC-AI

    这个是Google Brain大牛Jeff dean讲课视频的ppt,讲述了目前google目前在人工智能方面的研究和进展。视频地址 https://www.youtube.com/watch?v=HcStlHGpjN8&feature=youtu.be

    Quote of Jeff Dean

    这些内容主要是关于Jeff Dean的一些夸张和幽默的描述,以此为背景,我们可以提取知识点如下: 1. P=NP问题:文档中提到了著名计算机科学问题P=NP,这是关于计算机算法理论中的一个核心问题,它问的是:是否每个能在...

    JsUnpacker:迪恩·爱德华兹(Dean Edwards)JavaScript Packer解码器

    JsUnpacker是一个Java类,用于解码Dean EdwardsJavaScript Packer。 用法 JsUnpacker jsUnpacker = new JsUnpacker ( " eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]...

    The tail at scale by jeff dean

    在大规模服务系统设计领域,Jeff Dean的名字是如雷贯耳的存在,作为Google技术的高级副总裁,他不仅在分布式系统和大规模计算领域有着深厚的研究,而且亲自参与了多个重要系统的构建,其中包括Google的搜索引擎、大...

    jQuery-1.9.1源码分析系列(十)事件系统之事件体系结构

    jQuery的事件系统,特别是1.9.1版本,它在很大程度上受到了DeanEdwards的跨浏览器AddEvent()设计的影响。DeanEdwards的AddEvent()设计是一个早期尝试解决浏览器之间事件处理不兼容问题的方法。为了深入理解jQuery...

    Jeff-Dean-Large Scale Deep Learning

    在本段节选自Google工程师Jeff Dean与同事们合作研究的《大规模深度学习》文章中,描述了构建更智能计算机系统的复杂性以及大数据、机器学习、深度学习在其中扮演的角色。Jeff Dean是Google的高级研究员和副总裁,他...

    Jeff Dean在WSDM09的主题演讲_构建大规模信息检索系统中的挑战(中英文)

    **构建大规模信息检索系统中的挑战** 在信息技术领域,大规模信息检索系统是互联网...通过阅读"Jeff Dean_WSDM09-keynote.pdf"和"Jeff Dean_WSDM09-keynote_ch.ppt",读者可以深入了解这些关键概念和技术的详细信息。

    Free Dean's Office for MOODLE-开源

    《开源软件在教育领域的应用——以“Free Dean's Office for MOODLE”为例》 开源软件,作为一种共享、协作的软件开发模式,近年来在各个领域得到了广泛应用,尤其在教育行业中,其开放性、灵活性和成本效益使其...

    The Essence of Agile - Dean Leffingwell

    ### 敏捷的本质——迪恩·莱芬威尔 #### 知识点一:敏捷方法的概念与实践 **概念概述:** 迪恩·莱芬威尔在《敏捷的本质》中提出,从概念上讲,敏捷是非常简单的。它改变了软件开发和项目管理的方式。...

    Elements of the Theory of Computation 2nd计算理论高清版

    Gordon McKay Professor of Computer Science Harvard University and Dean of Harvard College Cambridge, Massachusetts Christos H. Papadimitriou C. Lester Hogan Professor of Electrical Engineering and ...

    js继承 Base类的源码解析

    // timestamp: Tue, 01 May 2007 19:13:00 /* base2.js – copyright 2007, Dean Edwards http://www.opensource.org/licenses/mit-license */ // You know, writing a javascript library is awfully time ...

    Jeff Dean - Large Scale Deep Learning with TensorFlow

    ### Jeff Dean - 大规模深度学习与TensorFlow #### 背景介绍 Jeff Dean是一位在人工智能领域享有盛誉的专家,他在Google Brain团队中担任重要角色。本篇演讲主要聚焦于利用大规模数据集和计算资源推动神经网络技术...

    Linux Device Drivers(3rd Edition, by Dean) 中文非影印版.pdf

    ### Linux Device Drivers(3rd Edition, by Dean) 中文非影印版 #### 知识点概述 **《Linux Device Drivers(3rd Edition, by Dean)》** 是一本关于Linux设备驱动开发的经典书籍,该书由Jonathan Corbet、...

    Architects of Intelligence: The truth about AI from the people building it

    Bestselling author Martin Ford talks to a hall-of-fame list of the world’s top AI experts, delving into the future of AI, its impact on society and the issues we should be genuinely concerned about ...

    jeff dean大神本科学位论文

    Jeff Dean本科学位论文,阐述了分布式ANN训练方案。对后续Tensor flow有参考。

Global site tag (gtag.js) - Google Analytics