0

I would like to know if there is a difference between the following in JavaScript:

function Person(name, family) {
    this.name = name;
    this.family = family;
}

Person.prototype.getFull = function() {
    return this.name + " " + this.family;
};

and

function Person(name, family) {
    this.name = name;
    this.family = family;
    Person.prototype.getFull = function() {
       return this.name + " " + this.family;
    };
}
Max Millington
  • 4,378
  • 4
  • 27
  • 34
  • 1
    right, it works. but it overwrites for every instance. – Nina Scholz Oct 12 '18 at 15:47
  • the 2nd one re-declares the method for all instance upon each instance creation, which is not ideal, even if it works (since there's no closures) – dandavis Oct 12 '18 at 15:47
  • Like pointed out the second one is re-creating the method everytime,. You could of course check if the method exists then create, but then it's getting untidy,.. So the first method is norm.. ps, if you find the first one tedious, you can always create an alias,.. eg.. `const $p = Person.prototype; $p.getFull = ....; $p.something = .... etc.`. And of course another idea is to use ES6 classes, and it will do the prototype bit for you. – Keith Oct 12 '18 at 15:51

0 Answers0