Take the 2-minute tour ×

Below are some code from cocos2dx-js binding official javascript sample

cc.Class = function(){};
cc.Class.extend = function (prop) { //John Resig's extend
    var _super = this.prototype;

    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;

    // Copy the properties over onto the new prototype
    for (var name in prop) {
        // Check if we're overwriting an existing function
            (function (name, fn) {
                return function () {
                    var tmp = this._super;

                    // but on the super-class
                    this._super = _super[name];

                    // remove it when we're done executing
                    var ret = fn.apply(this, arguments);
                    this._super = tmp;

                    return ret;
            })(name, prop[name]) :

    // The dummy class constructor
    function Class() {
        // All construction is actually done in the init method
        if (!initializing && this.ctor)
            this.ctor.apply(this, arguments);

    // Populate our constructed prototype object
    Class.prototype = prototype;

    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;

    return Class;

var Explosion = cc.Sprite.extend({
   ctor:function () {

   destroy:function () {

Explosion.sharedExplosion = function () {

Just wonder why sharedExplosion is putted outside the definition of var Explosion

share|improve this question
doesn't appear to be a particular reason. Normally when you see a pattern like that they're putting the function on the prototype of a constructor, but thats not happening here. –  Ben McCormick Jun 13 '13 at 1:53
We don't know either? But notice that this way it gets a "static" property of the constructor function object, not a property that is inherited by instances. –  Bergi Jun 13 '13 at 1:53
It saves tabs, indenting the sharedExplosoin within the explosion declaration adds another tab to the start of every line in sharedExplosion body. I read somewhere in Closuere: The Devinitive Guide that google has a max line with of 80 so developers there use this syntax a lot to save tabs. –  HMR Jun 13 '13 at 2:25
@HMR: Definitely not. It produces a different result, so you cannot just interchange the two "notations"! And one does program with the aim of good code, not of minimal tabulator usage! –  Bergi Jun 13 '13 at 2:57
Bergi is right, a "static" property of the constructor function object can not be inherited by instances. But I do not know why they wrote as these –  arachide Jun 13 '13 at 3:39
show 2 more comments

1 Answer

up vote 0 down vote accepted

It's a simple extension of Explosion outside its declaration. It is just an implication, I don't think there has to be particular reason for doing it.

share|improve this answer
add comment

Your Answer


