就把它当成面向对象不就行了嘛,反正有prototype这玩意

解决方案 »

  1.   

    无非就是底层模块给你提供这些功能(如java,c#.net)和自己实现这些功能的区别(js)
    扣概念,多此一举
      

  2.   

    其它的就那样,主要是继承,且js的继承玩出了花样,玩出了新意。虽然好玩,但比较难控。
    简单是这样玩的:function Base(){
            f1 = function (){
                console.log('f1 in base');
            };
            f2 = function(){
                console.log('f2 in base.');
            };
        }
        Base.prototype = {
            f3: function(){
                console.log('f3 in base.');
            },
            f4: function(){
                console.log('f4 in base.');
            }
        }
        function Child(){
        }
        function Extend(subFn, superFn){
            subFn.prototype = new superFn();
            subFn.prototype.constructor = subFn;
        }
        function Animal(){
            this.say1 = function (){
                console.log('Animal');
            }
        }
        function Tiger(){
            this.say2 = function (){
                console.log('tiger');
            }
        }
        Extend(Tiger, Animal);
        var tiger = new Tiger();
        tiger.say1();
        tiger.say2();复杂是这样玩的:// 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 consuming.
    //////////////////// BEGIN: CLOSURE ////////////////////
    // =========================================================================
    // base2/Base.js
    // =========================================================================
    // version 1.1
    var Base = function(){
        // call this method from any other method to invoke that method's ancestor
    };
    Base.prototype = {
        extend: function(source){
            //参数大于一个时
            if (arguments.length > 1) { // extending with a name/value pair
                //获得proto的祖先
                var ancestor = this[source];
                var value = arguments[1];
                //如果value(第二个参数)是function,并且祖先对象存在,在重载函数中调用base时
                if (typeof value == "function" && ancestor && /bbaseb/.test(value)) {
                    // get the underlying method
                    var method = value;
                    // override
                    value = function(){
                        var previous = this.base;
                        this.base = ancestor;
                        //上溯到父类对象
                        var returnValue = method.apply(this, arguments);
                        this.base = previous;
                        return returnValue;
                    };
                    value.method = method;
                    value.ancestor = ancestor;
                }
                this[source] = value;
            }
            else
            if (source) { // extending with an object literal 用一个对象列表来扩展
                var extend = Base.prototype.extend;
                /**
                 * 1.扩展原型方法和属性 2.
                 */
                //如果是扩展属于原型的方法或属性,先遍历其重载Object的3个方法
                if (Base._prototyping) {
                    var key, i = 0, members = ["constructor", "toString", "valueOf"];
                    while (key = members[i++]) {
                        //如果是重载了这些方法
                        if (source[key] != Object.prototype[key]) {
                            /**
                             * 逐个扩展,用call的原因是要将extend的上下文改为要扩展的源this,
                             * 既是新建对象的父类对象
                             */
                            extend.call(this, key, source[key]);
                        }
                    }
                }
                else
                if (typeof this != "function") {
                    // if the object has a customised extend() method then use it
                    extend = this.extend || extend;
                }
                // copy each of the source object's properties to this object
                for (key in source)
                    if (!Object.prototype[key]) {
                        extend.call(this, key, source[key]);
                    }
            }
            return this;
        },
        base: Base
    };
    Base.extend = function(_instance, _static){ // subclass
        /**
         * Base类原型的扩展别名,将这个当成一个方法调用
         */
        var extend = Base.prototype.extend;
        /**
         * build the prototype,创建原型
         * 设置原型标志
         */
        Base._prototyping = true;
        /**
         * 创建一个Base的实例,初始化继承部分
         * 继承方式大致还是以下方式
         * function A(){}
         * function B(){
        * this.b=[];
        * }
         * A.prototype=new B();//A继承B的所有属性和方法
         * 这种继承方式会有一个问题,B中声明的对象(如b)以prototype的形式
         * 被A继承之后,prototype只是生成一个指向B中对象的引用,即
         * A所有实例会共同享有B中对象(b)
         * var a1=new A();
         * var a2=new A();
         * a1.b.push("a11");
         * a2.b.push("a21");
         * 此时,a1.b=a2.b=["a11","a21"],
         *
         * Dean Edwards在实现继承的时候,以父类为基础,创建实例,
         * 利用extend扩展该实例,最后用A.prototype=new B();实现继承
         * 但是属性是对象的时候没有做处理,
         * 还是没有避开上述的继承缺陷
         */
        var proto=new this;
        /**
         * 在这里,不可以用 proto.extend(_instance)代替
         */
        extend.call(proto, _instance);
        /**
         * 类实例属性和方法的原型部分构造完毕,删除标志位
         */
        delete Base._prototyping;
        /**
         * 这里作者运用了适配器的模式,用自定义的构造器生成一个新的类对象
         * wrapper/adapter:通过一定的方法,一个对象封装或授权另一个
         * 对象来改变它的接口或者行为
         */
        // create the wrapper for the constructor function
        /**
         * 获得构造器的引用
         */
        var constructor = proto.constructor;
        /**
         * 建立klass的Function对象,调用自定义的构造器, klass就是衍生的子类
         * 两种情况下,调用此方法:
         * 1.创建类实例的时候,这时候不是原型构造阶段,执行由extend方法
         * 继承的时候设定的构造方法
         * 2.当用extend方法衍生子类的时候---new this
         * 因为下文中klass的属性已经全部获得,
         * 所以当new完之后,获得所有父类的方法和属性都包含在了
         * proto里面了,这时候,在proto的基础上运用prototype的extend方法
         * 将此子类的属性和方法添加到proto里面
         */
        var klass = proto.constructor = function(){
            /**
             * var proto=new this; 调用父类的构造函数,创建一个父类的实例
             * new this用完后,函数重定向到子类对象构造方法
             */
            if (!Base._prototyping) {
                /**
                 * 当在构造函数中(constructor)调用base方法时,
                 * base方法会调用父类对象的构造函数,这时候会嵌套
                 * 调用这个代码段,方法得以执行的条件就是this._constructing==true
                 */
                if (this._constructing || this.constructor == klass) { // instantiation
                    this._constructing = true;
                    constructor.apply(this, arguments);
                    delete this._constructing;
                }
                /**
                 *
                 * 不再向下执行
                 */
                else { // casting
                    var object = arguments[0];
                    if (object != null) {
                        (object.extend || extend).call(object, proto);
                    }
                    return object;
                }
            }
        };
        // build the class interface
        /**
         *
         */
        for (var i in Base){
            klass[i] = this[i];
        }
        /**
         * 创建继承链
         */
        klass.ancestor = this;
        klass.base = Base.base;
        klass.prototype = proto;
        klass.toString = this.toString;
        /**
         * 扩展类方法,属性,类似java的static
         */
        extend.call(klass, _static);
        // class initialisation 如果存在init函数 调用
        if (typeof klass.init == "function")
            klass.init();
        return klass;
    };
    // initialise
    Base = Base.extend({
        constructor: function(){
            this.extend(arguments[0]);
        }
    }, {
        ancestor: Object,
        base: Base,
        implement: function(_interface){
            if (typeof _interface == "function") {
                // if it's a function, call it
                _interface(this.prototype);
            }
            else {
                // add the interface using the extend() method
                this.prototype.extend(_interface);
            }
            return this;
        }
    });牛逼是这样玩的:(function () {
            var initializing = false,
                fnTest = /xyz/.test(function () {
                xyz;
            }) ? /\b_super\b/ : /.*/;
            this.Class = function () {};
            Class.extend = function (prop) {
                var _super = this.prototype;
                initializing = true;
                var prototype = new this();
                initializing = false;
                for (var name in prop) {
                    prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? (function (name, fn) {
                        return function () {
                            var tmp = this._super;
                            this._super = _super[name];
                            var ret = fn.apply(this, arguments);
                            this._super = tmp;
                            return ret;
                        };
                    })(name, prop[name]) : prop[name];
                }
                function Class() {
                    if (!initializing && this.init)
                        this.init.apply(this, arguments);
                }            Class.prototype = prototype;
                Class.constructor = Class;
                Class.extend = arguments.callee;
                return Class;
            };
        })();    Class.extend({
            name : 'jikey'
        });
      

  3.   

    先挖个坑,一个一个下来。
    先放2个链接,有空再看。
    http://zh.wikipedia.org/wiki/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1
    http://zh.wikipedia.org/wiki/%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E8%8C%83%E5%9E%8B
      

  4.   

    --------
    在维基百科中,还有两篇
    http://zh.wikipedia.org/wiki/JavaScript
    http://zh.wikipedia.org/wiki/%E5%8E%9F%E5%9E%8B%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88
      

  5.   

    面向对象 是一种思维模式
    面向对象编程 是一种编程方法javascript 既支持面向对象也支持面向过程
      

  6.   

    赞成版主的观点,javascript是具备了面向对象的特性。