Prototypes in JavaScript - FunFunFunction #16
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about JavaScript prototypes, constructors and Object.create, as a short explanation of prototypal inheritance for people coming from class-based languages.
Recommended by 3 people across 3 different months, 2016 to 2017.
What it is
An 11-minute episode of Fun Fun Function in which Mpj explains JavaScript prototypes, using a food prototype that produces waffle and carrot instances. He builds objects with Object.create and urges viewers to set aside class-based thinking.
What people said
Strong rec Tom · 2016-11 · Stack Overflow · on “How to set the constructor of an Object without changing to a new Object?”
myClass = { constructor: function(text){ console.log(text); } } let ob = Object.create(myClass); ob.constructor("old constructor "); //Output: old constructor myClass.constructor = function(){ console.log("new constructor ") } ob.constructor(); //Output: new constructor You can do this with every property or function of a class. Watch this Video here [this video] . It explains it perfectly.
Recommends Vala · 2017-03 · Stack Overflow · on “Is it possible to call constructor function method without creating object in JS?”
It appears that you are coming to JavaScript hoping that it's Java for the browser. It isn't. You should spend some time trying to grok the prototype inheritance model, which is pretty significantly different from classical inheritance. He's pretty judgmental in many ways and don't take his word as gospel, but I think you may pick up a lot from these videos: [this video] [LINK]
Recommends Mahdi Pedram · 2017-04 · Stack Overflow · on “Javascript Design Pattern Prototype error for web3.js”
You should use prototype when you want to create one or more instances of a function. in your case initweb3 is a constructor function. make sure your constructor function starts with uppercase letter. For example: function Person(){ /*...*/ } Person.prototype.sayHi = function() { }; const p = new Person(); p.sayHi(); in your use case persona is already an instance object , if you want to add a new function to it you simply do persona.testing = function() {} OR You can also try extending the initweb3 function. initweb3.prototype.testing = function(){ /* code goes here */} . . . persona.testing(); Watch this video to learn more about prototypes in JavaScript. [this video]
Cites sorenymous · 2016-05 · Stack Overflow · on “ES6 const for creating object prototypes in JavaScript; is this a pattern?”
I often see const being used to create a base object. Is this a pattern? If yes, what are the advantages? Example from here [this video] : const food = { init: function(type) { this.type = type; } } const waffle = Object.create(food); waffle.init('waffle'); console.log(waffle.type);