0

When I define a javascript class with methodes and submethodes this way it works:

function Controller () {
    this.OrdersSyncFreq = 3000; //ms

    this.syncOrders = function () {};
    this.syncOrders.start = function () { console.log("start was called"); };
    this.syncOrders.stop = function () { console.log("stop was called"); };
}

But how can I define the function Controller.syncOrders.start() later using "prototype"? Something like this does not work:

Controller.prototype.syncOrders.stop = function () {
    console.log("The NEW stop was called");
}
Lutz
  • 770
  • 1
  • 8
  • 19
  • 2
    No, this never really worked, you cannot use `this` in those "methods". Just don't do it. Use regular prefixes. – Bergi Jan 21 '17 at 15:18
  • This is the reason why i use .bind(this) in my own try of an answer. What do you mean with "regular prefixes"? – Lutz Jan 21 '17 at 15:26
  • Just call the methods `.syncOrdersStop` and `.syncOrdersStart` (or with underscores). – Bergi Jan 21 '17 at 15:31
  • Yes, but this is what i have recommended in my own answer as type of conclusion! Which you have "-1"ed – Lutz Jan 21 '17 at 15:33

1 Answers1

-1

Looking a bit more around, i found out it can be written like this:

function Controller () {
    this.OrdersSyncFreq = 3000; //ms
    this.syncOrders();  // have to be called at init to make sure the definitions of start and stop will be active. 
}

Controller.prototype.syncOrders = function () {

    this.syncOrders.start = function () {
        console.log("Start was called, frequence is: ",this.OrdersSyncFreq);
    }.bind(this);   // .bind(this)  is needed to have access this of your controller instance


    this.syncOrders.stop = function () {
        console.log("Stop was called, frequence is: ",this.OrdersSyncFreq);
    };

}

// run the code
var app = new Controller();
app.syncOrders.start();  // console: Start was called, frequence is:  3000
app.syncOrders.stop();  // console: Stop was called, frequence is:  undefined

The methodes of syncOrders - Start() and Stop() do not need to be prototyped, because syncOrders will not instantiated.

Anyhow I'm not sure if it really make sense to do it like this. I did it only because of the namespacing. may be it is better to use something more simple like syncOrdersStart() and syncOrdersStop() instead.

Lutz
  • 770
  • 1
  • 8
  • 19
  • This overwrites `syncOrders.start` and `syncOrders.stop` every time a new instance is created. – Bergi Jan 21 '17 at 15:19
  • yes this is ok. it will be instantiated only 2 times in my case. Anyhow, was the reason why i have written "I'm not sure if it really make sense"... – Lutz Jan 21 '17 at 15:23
  • BTW: I have written my question about 20 minutes. Than i found something like a solution. So to don't get negative points here (because of duplicate question or what ever), I decided to answer the question in which i still invested time by my self and invested again 30 minutes. And now i get negative points for this. this is no good learning! – Lutz Jan 21 '17 at 15:30
  • Problem starts when this answer hangs around and people start finding wrong answers, through search engines, marked as correct just for keeping points. THEN, you'll have bad learning experience. – Tim Vermaelen Jan 21 '17 at 15:32
  • You don't loose points because of unanswered or duplicate questions. An answer gets downvotes if it is considered bad advise by someone, or when the proposed solution does not work. – Bergi Jan 21 '17 at 15:33
  • No, this is not ok. `var a = new Controler, b = new Controler; b.OrdersSyncFreq = 6000; a.syncOrders.start()` - guess what this will log. If you absolutely want to use this syntax, [you have to create a new `syncOrders` for every `Controler` instance](https://stackoverflow.com/questions/15884096/organize-prototype-javascript-while-perserving-object-reference-and-inheritance). – Bergi Jan 21 '17 at 15:36
  • to be sure if have tied it. a.syncOrders.start() will log "Start was called, frequence is: 3000", b.syncOrders.start() will log "Start was called, frequence is: 6000". This is exactly what i need. the sync times for both instances need to be able to be different, because a and b will be used to sync different types of data. – Lutz Jan 21 '17 at 15:46
  • I sync the amazon (b) orderlist from and (a) orderitems. The order list (which holds 100 orders) need to be refreshed only once a minute, but the orderitems request need to be done each 2 seconds. because it delivers only the items for 1 order (and throttling applies). – Lutz Jan 21 '17 at 15:54