zinherit源码地址:
http://www.nczonline.net/downloads/zInherit1.0.zip
<HTML>
    <HEAD>
        <TITLE>test </TITLE>
    </HEAD>
    <SCRIPT LANGUAGE="JavaScript">
        Object.prototype.inheritFrom = function (fnClass)  
{
            /**
             * Inherits all classes going up the inheritance chain recursively.
             * @param fnClass The class to inherit from.
             * @param arrClasses The array of classes to build up.
             * @scope private
             */
            function inheritClasses(fnClass,arrClasses)
         {
                arrClasses.push(fnClass);
//alert("type:"  + typeof fnClass.__superclasses__ );
                if (typeof fnClass.__superclasses__ == "object") 
{
                    for (var i=0; i < fnClass.__superclasses__.length; i++)
{
                        inheritClasses(fnClass.__superclasses__[i], arrClasses);
                    }
                }
            }
            
            if (typeof this.constructor.__superclasses__ == "undefined") 
{
                this.constructor.__superclasses__ = new Array();
            }
            
            inheritClasses(fnClass, this.constructor.__superclasses__);
            //所有用prototype方式声明的函数将赋给新实例
            for (prop in fnClass.prototype) 
{
                if (typeof fnClass.prototype[prop] == "function")
{
                    this[prop] = fnClass.prototype[prop];
                }
            }
        };
        
        /**
         * Determines if the given object is an instance of a given class.
         * This method is necessary because using {@link #inheritFrom} renders
         * the JavaScript <code>instanceof</code> operator useless.
         * @param fnClass The constructor function to test.
         * @return True if the object is an instance of the class, false if not.
         * @scope public
         */
        Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */
{
        
            if (this.constructor == fnClass) 
{
                return true;
            } 
else if (typeof this.constructor.__superclasses__ == "object") 
{
                for (var i=0; i < this.constructor.__superclasses__.length; i++) 
{
                    if (this.constructor.__superclasses__[i] == fnClass) 
{
                        return true;
                    }
                }
                return false;
            } 
else 
{
                return false;
            }
        };
        
    </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript">
        <!--
        function Polygon(iSides){
            this.sides = iSides;
        }
        
        Polygon.prototype.getArea = function(){
            return 0;
        };
        
        function Triangle(iBase, iHeight){
            Polygon.call(this, 3);
            this.base = iBase;
            this.height = iHeight;
        }
        
        Triangle.prototype.inheritFrom(Polygon);
        
        var t = new Triangle(2, 8);
        alert(t.instanceOf(Triangle));
        alert(t.instanceOf(Polygon));
        //-->
    </SCRIPT>
    <BODY>
    </BODY>
</HTML>对其中的inheritClasses不是理解,其中 inheritClasses(fnClass.__superclasses__[i], arrClasses);
其中fnClass.__superclasses__属性并不存在的,而只有 this.constructor.__superclasses__ = new Array();
难道fnClass.__superclasses__ == this.constructor.__superclasses__  ?

解决方案 »

  1.   

    我用ff进行调试的过程中发现:alert("type:"  + typeof fnClass.__superclasses__ );
                    if (typeof fnClass.__superclasses__ == "object") 
    {
       alert('in:' + fnClass.__superclasses__);
                        for (var i=0; i < fnClass.__superclasses__.length; i++)
    {
                            inheritClasses(fnClass.__superclasses__[i], arrClasses);
                        }
                    }
    fnClass.__superclasses__ 的类型总是"undefined",到底这段代码起什么作用,什么时候起作用?
      

  2.   


        Object.prototype.inheritFrom = function (fnClass)  
    {
                /**
                 * Inherits all classes going up the inheritance chain recursively.
                 * @param fnClass The class to inherit from.
                 * @param arrClasses The array of classes to build up.
                 * @scope private
                **/
                function inheritClasses(fnClass,arrClasses)
             {
                    arrClasses.push(fnClass);
    //alert("fnClass:" + fnClass + ",type:"  + (typeof fnClass.__superclasses__ ));
                   /** if (typeof fnClass.__superclasses__ == "object") 
    {
       alert('in:' + fnClass.__superclasses__);
                        for (var i=0; i < fnClass.__superclasses__.length; i++)
    {
                            inheritClasses(fnClass.__superclasses__[i], arrClasses);
                        }
                    }**/
                }
                
    alert("constructor:" + this.constructor + "superclasses__:" + this.constructor.__superclasses__);
    //把所有的父类都放进一个constructor.__superclasses__的一个属性(数组)中,便于instanceOf使用
                if (typeof this.constructor.__superclasses__ == "undefined") 
    {
                    this.constructor.__superclasses__ = new Array();
                }
                
                inheritClasses(fnClass, this.constructor.__superclasses__); 
                //所有用prototype方式声明的函数(方法)将赋给新实例
                for (prop in fnClass.prototype) 
    {
                    if (typeof fnClass.prototype[prop] == "function")
    {
                        this[prop] = fnClass.prototype[prop];
                    }
                }
            };
            
            /**
             * Determines if the given object is an instance of a given class.
             * This method is necessary because using {@link #inheritFrom} renders
             * the JavaScript <code>instanceof</code> operator useless.
             * @param fnClass The constructor function to test.
             * @return True if the object is an instance of the class, false if not.
             * @scope public
             */
            Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */
    {
            
    alert("constructor:" + this.constructor + ",fnClass:" + fnClass);
                if (this.constructor == fnClass) 
    {
                    return true;
                } 
    else if (typeof this.constructor.__superclasses__ == "object") 
    {
    alert("typeof:" + typeof this.constructor.__superclasses__);
                    for (var i=0; i < this.constructor.__superclasses__.length; i++) 
    {
    alert("this.constructor.__superclasses__[i] :" + this.constructor.__superclasses__[i] );
                        if (this.constructor.__superclasses__[i] == fnClass) 
    {
                            return true;
                        }
                    }
                    return false;
                } 
    else 
    {
                    return false;
                }
            };
            
        </SCRIPT>被修改后的zInherit源码,没发现任何问题.
    请注释中的部分起什么作用?
      

  3.   

    那个属性是为下面那个判断类型服务的,用原有的instanceof不能出判断继承关系,但是用这个提供的obj.instanceOf(Obj)就可以。