s_curry_s
s_curry_s

Reputation: 3432

What's the disadvantage of creating prototype methods inside the object?

I have seen people take two approaches when they create javascript objects, they sometimes define the prototype functions outside the main object and sometimes they do it inside. Defining the prototype functions inside the object has the advantage of using private variables and functions, what's the advantage of defining it outside?

function something (params) {
  this.name = params.name;
  this.color = params.color;
  _someprivatefunction = function () {};
  something.prototype.publicFunction = function () {_someprivatefunction() };
}

As opposed to:

function something (params) {
  this.name = params.name;
  this.color = params.color;
  _someprivatefunction = function () {};
}

something.prototype.publicFunction = function () {//can't call the private function here };

EDIT: as suggested in the comments down below, this is a third option (but the problem with this is now the private function cant access any of the private variables inside the constructor.)

(function () {
  function something (params) {
    this.name = params.name;
    this.color = params.color;
   }

   _someprivatefunction = function () {};

   something.prototype.publicFunction = function () {_someprivatefunction() };
 }());

Upvotes: 0

Views: 454

Answers (2)

jfriend00
jfriend00

Reputation: 707288

As I think you know, your two code blocks don't offer the same functionality. The first option allows you to call _someprivatefunction() or other private constructor variables and your second does not. So, first you have to decide if that's a requirement or not. Your two options do not offer equivalent functionality.

If you do want access to the private function, then a third option is as follows:

function something (params) {
    // initialize data members
    this.name = params.name;
    this.color = params.color;

    // initialize private members
    var aPrivateInstanceVar = 4;

    // define private function
    _someprivatefunction = function () { console.log(aPrivateInstanceVar);};

    // define public method with access to private members and functions
    this.publicFunction = function () {_someprivatefunction() };
}

This new option will technically work the same as your first option. But (and this might just be my opinion), it feels cleaner. It's assigning a property dynamically to the object at creation time which is a much more common thing to do (just like assigning to this.name and this.color) than assigning something to the prototype at object creation time.

Also, if I came across some uncommented code someone else wrote that has your first construction in it, my first thought would be: "gee, why isn't this assignment to the prototype done once outside the constructor" and I might even attempt to "fix" it without realizing that it needed to be there. But, if I came across the structure I'm proposing, it would like like a perfectly common design pattern and I would not be tempted to "fix" the code (accidentally breaking it in the process).

Upvotes: 1

TGH
TGH

Reputation: 39248

I would assume that a disadvantage to the first approach is that you are running the prototype assignment every time you instantiate a new object. Seems like it would defeat the purpose of using the prototype since it does work on the object for every instance created.

Upvotes: 2

Related Questions