0

I'm trying to use OOP in JavaScript, and from what I can tell there's a couple of different ways of doing that. With the one I'm using, I'm having difficulty calling an object's method within itself.

This style of OOP is the one that has everything I need in it, so the other styles are off the cards.

function Node(message,options) {
    this.message = message;
    this.options = options;

    this.activate = function() {
        SendMessage(message);
        this.specialInstructions();
    }

    this.getMessage = function() {
        return this.message;
    }

    this.getOptions = function() {
        return this.options;
    }

    this.specialInstructions = function() {
        SendMessage("Test");
    }
}

Here I'm trying to execute specialInstructions() inside of activate(), but whenever I try that it says it's undefined.

Harry
  • 29
  • 3
  • 3
    That should work just fine, depending on how `activate` is called (details [here](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) and [here](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work)). Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/)). – T.J. Crowder May 26 '19 at 14:24
  • 1
    *"This style of OOP is the one that has everything I need in it, so the other styles are off the cards."* Just FYI, that style doesn't work well with inheritance and doesn't take advantage of JavaScript's prototypical inheritance mechanism. Here in 2019, I'd either create a prototype object and builder function that used `Object.create` manually, or I'd use `class`. I wouldn't create functions in the constructor like that without a specific overriding need to. – T.J. Crowder May 26 '19 at 14:27
  • 1
    The error is probably in `SendMessage`. By the way, the sendmessage call does not seem OOP? It seems just a global function? Can you include SendMessage? – Kokodoko May 26 '19 at 14:34
  • The use of `Node` as an identifier is also going to cause you trouble if you use this in a browser context. `Node` is an existing object in the DOM and you will overwrite it with this code. – Randy Casburn May 26 '19 at 14:38
  • @T.J.Crowder I'll take a look at switching to ```Object.create``` before I pester you guys for more. Thanks for the info. – Harry May 26 '19 at 14:39
  • @Kokodoko ```SendMessage()``` is a global function, it's like, one line too. Haven't had any problems with it anywhere else in the code. – Harry May 26 '19 at 14:40
  • @RandyCasburn Ah, I didn't know that. That's kind of a pain. I'll switch over to another name. – Harry May 26 '19 at 14:41
  • @Harry - Just don't run your code at global scope. `Node` isn't the only global it's easy to conflict with. Run your code in a module, or within a scoping function, so the variables are local to the module or scoping function. – T.J. Crowder May 26 '19 at 14:43

0 Answers0