JavaScript What is the difference between Adding Methods to Javascript Prototype and Adding Functionality to Existing Objects what difference between bourbon and whiskey,what difference between vegan

I read in a book that we can extend functionality of existing objects using the following code:

var Point = function(x, y) {
    this.x = x; 
  this.y = y; 
}

Point.prototype.moveBy = function(deltaX, deltaY) {
    this.x += deltaX; 
  this.y += deltaY; 
}
Point.prototype.moveTo = function(otherPoint) {
    this.x = otherPoint.x; 
  this.y = otherPoint.y; 
}
var p1= new Point(100, 200); 
p1.moveBy(10, 20); 
var p2= new Point(25, 50); 
p2.moveTo(p1); 
alert("p2.x: " + p2.x + " p2.y: " + p2.y); 

I just found that I can get the same results with prototype as follows:

var Point = function(x, y) {
    this.x = x; 
  this.y = y; 
}

Point.prototype = {
    moveBy: function(deltaX, deltaY) {
    this.x += deltaX; 
  this.y += deltaY; 
    }, 

    moveTo: function(otherPoint) {
    this.x = otherPoint.x; 
  this.y = otherPoint.y; 
    }
}; 
var p1= new Point(100, 200); 
p1.moveBy(10, 20); 
var p2= new Point(25, 50); 
p2.moveTo(p1); 
alert("p2.x: " + p2.x + " p2.y: " + p2.y); 

So what is the difference? It did not make sense to me. What Object are we adding functionality to? What is the difference if we created the functions as methods in the Point object? Why don't we do it using Prototype only?

Answer:1

Members bound to Prototype are shared across instances. There is only one instance of that member.

function Point(value) { this.value = value; }
Point.prototype.moveBy = function() { console.log('from the prototype', this.value);};
var p1 = new Point('p1');
var p2 = new Point('p2');
p1.moveBy === p2.moveBy; //true. Same instance of moveBy function

Members set directly to this inside the constructor or set to a specific instance of Point is available to that specific instance of Point only. If you want to expose logic shared across all instances of Point, it's best to set it on the Prototype.

var p3 = new Point('p3');
p3.moveBy = function() { console.log('from the instance', this.value); };
p3.moveBy === p2.moveBy; //false

var p4 = new Point('p4');
p4.moveBy = function() { console.log('from the instance', this.value) };
p4.moveBy === p3.moveBy; //false

The cool part is that you can always access the prototype even if you have declared it on the instance. If so, pass the instance as context:

Point.prototype.moveBy.call(p3); //will call moveBy set on the prototype. Output: "from the prototype p3"
p3.moveBy(); //will call moveBy set on the instance. Output: from the instance p3

The above snippet illustrates fundamental concepts of JavaScript (the context) and object-oriented JavaScript (prototype). If the subject is of interest to you, take a look at N. Zackas' book on the subject: https://www.amazon.com/Principles-Object-Oriented-JavaScript-Nicholas-Zakas/dp/1593275404

Answer:2

I have a Grid and on grid context menu I am calling one function as follows- On grids context menu I have added the following item { text: 'Preview', handler: 'PreviewGrid', scope: cnt, }; In ...

I have a Grid and on grid context menu I am calling one function as follows- On grids context menu I have added the following item { text: 'Preview', handler: 'PreviewGrid', scope: cnt, }; In ...

For example: var array1 = ["one", "two", "three"]; var array2 = ["1", "2", "3"]; How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]?

For example: var array1 = ["one", "two", "three"]; var array2 = ["1", "2", "3"]; How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]?

I have a function that returns the filtered result based on string in the object (if it exist or not) let foo = nodes.reduce((arr, cur) => { cur.classes.split(' ').filter((el) => { ...

I have a function that returns the filtered result based on string in the object (if it exist or not) let foo = nodes.reduce((arr, cur) => { cur.classes.split(' ').filter((el) => { ...

I need to build a 2 way communication between a browser and an REST API service. One of the most complex thing I am facing is checking the API for updates every second. I have two types of ...

I need to build a 2 way communication between a browser and an REST API service. One of the most complex thing I am facing is checking the API for updates every second. I have two types of ...

  1. websocket communicate with each other
  2. websocket communication