Javascript methods in classes

u0206397

Senior Member
Joined
Jul 15, 2009
Messages
764
Reaction score
0
I notice there are more than 1 way to define methods in pure Javascript (not ES6, ES2016).

Style 1: Using Prototype

PHP:
var Example = function(data){
    this.data = data;
}

Example.prototype = {
    doSomething: function( parameter ) {
        alert( "Busy doing something" );
    }
}

Style 2: Returning an array within class definition

PHP:
var Example = function(data) {
    this.data = data;

    return {
        doSomething: function( parameter ) {
            alert( "Busy doing something" );
        }
    }
}

Any difference between the 2 styles? Or just stylistic difference? :s11:
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top