1

I have a good background in OOP, mostly in C++ and Java. I want to use objects in JavaScript, including inheritance. The articles I've read on JS inheritance always talk about the prototypal inheritance model. When creating functions (methods) for JS objects, they are always declared after the object itself is declared, and in a format such as: Object.prototype.funcName = function() { }. Yes, this works, but it also seems to work when declaring the function inside the object, like: this.funcName = function() { };

This is one of the articles I've read: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance.

I've tested my code both ways and they seem to work identically well. Can anybody tell me if I will encounter any problems down the road by declaring my functions inside my objects (like standard OOP), or will it bite me in the end?

Here's 'my' way:

function Person(name) {
  this.name = name;

  this.greeting = function() {
    console.log('Hi! I\'m ' + this.name + ' from Person');
  };
};

Here is the prototypal way:

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

Person.prototype.greeting = function() {
  console.log('Hi! I\'m ' + this.name + ' from Person.proto');
};

Creating the object and calling the greeting works either way:

var person = new Person("Cool Dood");

person.greeting();

Any insights as to whether or not 'my' way of declaring the functions inside the object declaration would cause me problems in the future?

tpdietz
  • 41
  • 4
  • 2
    This is kind-of tangential to your question, but it'll help you get used to JavaScript if you completely stop trying to relate how it works to C++ and Java. They're way, way different. – Pointy Mar 28 '19 at 21:50
  • 1
    And the main difference is that you're instantiating a new function for each instance you create. That can have advantages and disadvantages. – Pointy Mar 28 '19 at 21:51
  • Maybe this could help: https://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor – Shidersz Mar 28 '19 at 21:53
  • I would say that both methods are equally valid however it might be worth exploring ES5 and classes, for a more OOP approach, although I agree with Pointy that Javascript should be approached in a different way to languages like C++ and Java – Alessi 42 Mar 28 '19 at 21:54
  • @Pointy, I know that JS is a very different animal than C++ and Java. However, wanting to develop using OO principals is a choice and, to me, it's more logical to encapsulate everything in the same object, instead of declaring the functions outside of the properties. – tpdietz Mar 29 '19 at 14:48
  • @Shidersz, thank you for that link. it is very helpful. I did search before writing my post, but there were dozens of results and the ones that I read did not exactly answer my question. That link does. – tpdietz Mar 29 '19 at 14:49
  • @Alessi42, thanks for your suggestion. However, we need to support IE11, which does not support classes, otherwise, I'd love to go that route. – tpdietz Mar 29 '19 at 14:51

0 Answers0