Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using prototype JavaScript - best practice

Currently I am working in a project where we are writing Object Oriented JavaScript. In this project I have seen two different ways of defining a class:

1: Declare functions at once on the prototype

My.Namespace.ClassName = function(param1, param2) {
   this.member1 = param1;
   this.member2 = param2;
};

My.Namespace.ClassName.prototype = {
   myFunction1: function() {
      return this.member1 + " " + this.member2;
   },

   myFunction2: function(param1) {
      this.member3 = paraml;
   }
};

2:Prepare each function individual on the prototype

My.Namespace.ClassName = function(param1, param2) {
   this.member1 = param1;
   this.member2 = param2;
};

My.Namespace.ClassName.prototype.myFunction1 = function() {
   return this.member1 + " " + this.member2;
};

My.Namespace.ClassName.prototype.myFunction2 = function(param1) {
   this.member3 = paraml;
};

Is there any difference in how JavaScript behaves based on the two given examples or is it only a style difference?

Personally I haven't seen any behavior difference but I have the feeling that there must be a subtle difference which I am currently missing.

Besides that. I would like to know whether this is a common practice or are there much better ways to define classes.

like image 448
hwcverwe Avatar asked Nov 20 '14 15:11

hwcverwe


People also ask

Should you use prototype in JavaScript?

There is a clear reason why you should use prototypes when creating classes in JavaScript. They use less memory. When a method is defined using this. methodName a new copy is created every time a new object is instantiated.

Should I use prototype or class JavaScript?

To answer your question simply, there is no real difference. Straight from the MDN web docs definition: JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance.

What is the benefit of prototype in JavaScript?

Prototypes allow you to easily define methods to all instances of a particular object. The beauty is that the method is applied to the prototype, so it is only stored in the memory once, but every instance of the object has access to it.

Is __ proto __ deprecated?

prototype. __proto__ Deprecated: This feature is no longer recommended.


1 Answers

There is a subtle difference. In the first method, when you overwrite the prototype, there was a property there that is now lost. That's the constructor, which points back to your function. The constructor allows you to recreate the type of object that it is.

You can easily get it back and so use the first method by manually setting it:

My.Namespace.ClassName.prototype = {
   myFunction1: function() {
      return this.member1 + " " + this.member2;
   },

   myFunction2: function(param1) {
      this.member3 = paraml;
   },
   constructor: My.Namespace.ClassName
};

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

like image 172
Scimonster Avatar answered Oct 09 '22 15:10

Scimonster