user5419187
user5419187

Reputation:

methods calling methods inside object

i would like to know if there is a name for the pattern (is a pattern?) that i use. For example, instead of use like this:

var MyFakeClass = function(propertie) {
 this.propertie = propertie

 this.init();
};

MyFakeClass.prototype.method = function() {
 // something here
};

var instanceOfFakeClass = new MyFakeClass('propertie');
instanceOfFakeClass.method();

I do as follow:

var MyFakeClass = {
 init: function(propertie) {
  this.propertie = propertie;
  this.method();
 },

 method: function() {
  // something here
 }
};

MyFakeClass.init('propertie');

So, the init method calls the method, i don't need call from the outside.

Thanks.

Upvotes: 0

Views: 37

Answers (1)

jfriend00
jfriend00

Reputation: 707228

There are many different ways to describe it and probably no single moniker that is always used for that:

  1. Object literal
  2. Statically declared object
  3. Statically declared singleton

"Singleton" is probably a useful word here because it describes an object of which there is only intended to be one of. There are many different possible ways to declare a singleton. Your declaration is one such way.

"Statically declared" differentiates your second option from the first one with a constructor that is created with new.

Upvotes: 1

Related Questions