1

Background

Working with OOP-style in JavaScript, by creating pseudo-classes using objects. From examples and walkthroughs online, I'd been under the impression that we need to set up an object, then attach function to that object's prototype object.

However, I have been playing around today and tried attaching a function directly to the object itself. From testing, I can't find any behavioral differences. Simplified example of both ways below:

Version 1

var Class = function( params ) {
}

Class.prototype.getData = function() {
    return _data;
};

Version 2

var Class = function( params ) {
    this.getData = function() {
        return _data;
    }
}

Question

What is the difference, if any, between these two implementations?

kenda
  • 488
  • 6
  • 16
Kelderic
  • 6,502
  • 8
  • 46
  • 85
  • difference is in defining method on `Class` instance or on `Class` instance prototype ) – Maxx Sep 22 '16 at 12:40

0 Answers0