4 ms·
Lets compare the constructor: function A () { this.helloWorld = function () {return 'hello world'}; }; a = new A(); vs. function B () {}; B.prototype.he
by v413 16y ago
Lets compare the constructor:
function A () {
this.helloWorld = function () {return 'hello world'};
};
a = new A();
vs.
function B () {};
B.prototype.helloWorld = function () {return 'hello world'};
b = new B();
Both constructors achieve the same result - their instances a and b both have the methods helloWorld.
Some of the differences are:
1. With constructor A, whenever you create a new instance, you create a new function, hence you have both performance and memory inefficiencies. With Constructor B, the helloWorld function is created just once, and every instance of B automatically inherits this single instance of the function helloWorld, hence we have performance and memory efficiency.
2. Using inheritance through the prototype of the constructor functions also gives more flexibility and simplicity in sharing common functions and modules.