简体   繁体   中英

Inherit properites in javascript “object”

Folks, can you please help me with this questions.

var Module = function ( options ) {
var settings = {
    selector: 'body',
    declaration: function () {
        return{
            init: function () {
                console.log( 'nodule initialize' );
            },
            destroy: function () {
                console.log( ' module destroyed' );
            }
        }
     }
   };
this.declaration = options.declaration || settings.declaration;
this.selector = options.selector || settings.selector;
};

var Registration = function ( options ) {
   this.selector = options.selector || **strong text**;
   this.declaration = options.declaration
}
app.utils.extend( Module, Registration );
var m_registration = new Registration( {
   declaration: function ( f ) {
      return {
        init: function () {
        },
        destroy: function () {
        }
    }
  }
} );

My main questions, how i can inherit Module.selector properties in m_registration, if we haven't need quantity arguments passed when we are creating instance of Registration

My realisation of function app.utils.extend():

var app.utils.extend = function ( from, to ) {
        var F = function () {
        };
        F.prototype = from.prototype;
        to.prototype = new F();
        to.prototype.constructor = to;
        to.superclass = from.prototype;
    }

Update :

If we are using these method:

var Registration = function ( options ) {
       Module.call(this, { selector : options.selector });
       this.declaration = options.declaration
}

How we can use inheritance if we really dont know from what instance this class extended or inheritance.

var Registration = function ( options ) {
   Module.call(this, { selector : options.selector || **strong text**});
   this.declaration = options.declaration
}

I don't know what **strong text** means in your case. But if you call it like this

var Registration = function ( options ) {
       Module.call(this, { selector : options.selector });
       this.declaration = options.declaration
    }

Or

var Registration = function ( options ) {
       Module.call(this, options );
       this.declaration = options.declaration
    }

In your case m_registration.selector will be 'body'

var Module = function ( options ) {
    this.declaration = options.declaration;
    this.selector = options.selector;
}

Module.prototype = {
    selector: 'body',
    declaration: function () {
        return{
            init: function () {
                console.log( 'nodule initialize' );
            },
            destroy: function () {
                console.log( ' module destroyed' );
            }
        }
     }
   };

Another approach is to put all the properties you want inherited to the prototype property. This is how javascript engine deals with inheritance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM