cocos2dx javascript google類繼承寫法

inherits = function (childCtor, parentCtor) { /** @constructor */ function tempCtor() {}; tempCtor.prototype = parentCtor.prototype; childCtor.superClass_ = parentCtor.prototype; childCtor.prototype = new tempCtor(); childCtor.prototype.constructor = childCtor;app

// Copy "static" method, but doesn't generate subclasses.

// for( var i in parentCtor ) { // childCtor[ i ] = parentCtor[ i ]; // } };this

base = function (me, opt_methodName, var_args) { var caller = arguments.callee.caller; if (caller.superClass_) { // This is a constructor. Call the superclass constructor. ret = caller.superClass_.constructor.apply(me, Array.prototype.slice.call(arguments, 1)); return ret; }prototype

var args = Array.prototype.slice.call(arguments, 2);
var foundCaller = false;
for (var ctor = me.constructor;
     ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
    if (ctor.prototype[opt_methodName] === caller) {
        foundCaller = true;
    } else if (foundCaller) {
        return ctor.prototype[opt_methodName].apply(me, args);
    }
}

// If we did not find the caller in the prototype chain,
// then one of two things happened:
// 1) The caller is an instance method.
// 2) This method was not called by the right caller.
if (me[opt_methodName] === caller) {
    return me.constructor.prototype[opt_methodName].apply(me, args);
} else {
    throw Error(
        'cc.base called from a method of one name ' +
            'to a method of a different name');
}

};code

var Fish = function( name , color ){ this.name = name; this.color = color; Fish.prototype.printName = function(){ console.log( this.name ) }it

}io

function FlyFish( name , color ){ base(this,name,color); FlyFish.prototype.printName = function(){ base(this,'printName'); console.log( "name is : " + this.name ); } }console

function FlyFireFish( name , color ){ base(this,name,color); FlyFireFish.prototype.printName = function(){ base(this,'printName'); console.log( "FlyFireFish is : " + this.name ); } }function

inherits(FlyFish,Fish);class

inherits(FlyFireFish,FlyFish);call

var fish1 = new FlyFish( "mackarel" , "gray" ); var fish2 = new Fish( "goldfish" , "orange" ); var fish3 = new FlyFireFish( "salmon" , "white" );

fish3.printName()

相關文章
相關標籤/搜索