-2

Here is a bit of non-working JavaScript code:

function clientEventsManager(io) {
    this.connectedClients = 0;

    this.createEventReceivers = function(io) {
        io.sockets.on('connection', function(socket) {
                this.connectedClients++;
                //does not increase "connectedClients" of "clientEventsManager" function
        }
    }

    createEventReceivers(io); //it is the only call to createEventReceivers()
}



var Manager = new clientEventsManager(io); //it is the only instanciation of clientEventsManager

My question is:
Is there a way to change clientEventsManager.connectedClients in clientEventsManager.createEventReceivers()?

EDIT: this post is a duplicate of this one, thank's for answering

Community
  • 1
  • 1
Magix
  • 4,989
  • 7
  • 26
  • 50
  • 2
    You should be able to just fine: http://jsfiddle.net/Aa277/. How are you calling the `fct1` method? The code is kind of "confusing" because `base` isn't capitalized (to signify a constructor). Are you just calling `base`, or are you constructing a new `base` and calling that instance's `fct1` method?? – Ian Jun 18 '14 at 16:48
  • 1
    *"Is there a way to get the `attr1` attribute of base function in `base.fct1` function?"* Yes, many (well, maybe 4). Learn how `this` works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this – Felix Kling Jun 18 '14 at 16:48
  • Thanks for updating. I already assumed that it will be a **duplicate** of [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) (can't vote anymore though) – Felix Kling Jun 18 '14 at 17:05
  • I'd be curious to know why you're putting the `createEventReceivers` function directly on the object in the constructor instead of on `clientEventManager.prototype`. – cookie monster Jun 18 '14 at 17:10
  • I did that because the Manager will be instanciated only once. Knowing that, I thought putting it directly in the constructor was better for reading and for best code folding... am I wrong to do that? – Magix Jun 18 '14 at 17:13
  • If there'll only be a single `Manager` instance, then a constructor function isn't really needed at all... or at least you could do it without adding a name to the variable scope. The simplest solution would be just to use an object literal, and have the callback reference that resulting object. There are other approaches as well. There are other approaches too. – cookie monster Jun 18 '14 at 17:17
  • [Here's a demo](http://jsfiddle.net/Yg3m8/) using an object literal. And [here's one](http://jsfiddle.net/Yg3m8/1/) using what's usually called a module pattern. – cookie monster Jun 18 '14 at 17:22
  • Please look at my edit - an important line I forgot. The function clientEventsManager.createEventReceiver() is in fact called when new clientEventsManager is invoked... does it change anything? – Magix Jun 18 '14 at 17:41
  • Given your current code, you'd need `this.` before the function call. I don't know which parts of the code are meant to be used once, and which parts are used multiple times, so I don't know the overall best approach. – cookie monster Jun 18 '14 at 17:48

1 Answers1

-2

Quick and dirty solution

function base(param) {
    this.attr1 = "azertyuiop";
    this.attr2 = 123;
    this.attr3 = param;
    var self=this;
    this.fct1 = function() {
        console.log("azerty keyboard first row: "+self.attr1);
        //Doesn't work
    }
}
ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • 2
    Why? What problem is it solving? – cookie monster Jun 18 '14 at 16:51
  • getting the value of attr1, which isn't working. Just read the question before downvoting, you'll save a ton of clicks. – ffflabs Jun 18 '14 at 16:52
  • 1
    @amenadiel: Why is it not working? – Felix Kling Jun 18 '14 at 16:53
  • I downvoted your answer because it's not very good. There's no problem given in the question, and so this as a "solution" is odd. We have no idea if the function should keep a permanently bound reference to `this`. Nor do we know what the value of `this` actually is. – cookie monster Jun 18 '14 at 16:54
  • 2
    @user3753446: You might not be able to do that, but what you certainly can is **clarify your question** and provide a **complete** example. – Felix Kling Jun 18 '14 at 16:56
  • 1.- Because OP said so and I don't see a reason to judge him a liar, even if he's a confessed french. And 2.- Because the example might not be the exact situation OP is seeing, and whatever it is, I believe it's more helpful to store a reference to ```this``` in an auxiliary variable instead of reccomending the user to study variable scopes in JS. – ffflabs Jun 18 '14 at 16:57
  • @amenadiel: FWIW, no one suggested the OP to learn about variable scope (but it's still a good thing to know). – Felix Kling Jun 18 '14 at 16:58
  • 1
    @amenadiel: He didn't say it doesn't work. Far as we know, he never tried it. Also, if `this` is the global object, then he's doing far more wrong. Finally, sometimes the solution is to permanently bind the `this` value, sometimes not. – cookie monster Jun 18 '14 at 16:59
  • @amenadiel: Then you don't know the difference between `this` and scope in JS. – cookie monster Jun 18 '14 at 16:59
  • 2
    @amenadiel: I said to learn about `this`, which has nothing to do with variable scope. If one wants to use `this`, they should know how it works, shouldn't they? – Felix Kling Jun 18 '14 at 16:59
  • You mean ```this``` doesn't have a scope that can change depending of the context? – ffflabs Jun 18 '14 at 17:00
  • 1
    @MagixKiller: It all depends on the actual problem. It's rare to create separate functions on `this` in a constructor *(if indeed your function is used as a constructor)*. More often you'd use prototypal inheritance. JavaScript is very flexible, and there are often multiple solutions to a problem. But the overall nature of the problem is important to know. – cookie monster Jun 18 '14 at 17:02
  • 2
    `this` is an implicitly defined local "variable" in every function, just like `arguments`. Of course `this` has scope, it's local to every function, but I would argue that the "problems" that arise with `this` don't actually have anything to with scope. All you need to know about scope with regard to `this` is that inner variables with the same name shadow outer variables. – Felix Kling Jun 18 '14 at 17:02
  • Anyways, we digress. While your answer may "work", since we don't know how the code is actually used, what `this` actually refers to, inside `base` and inside `fct1`, you answer is just an (educated) guess. – Felix Kling Jun 18 '14 at 17:05
  • See the OP edition. In fact, his example wasn't accurate. But I gave him a quick and dirty solution to solve those cases in which you need to address the class instance ```this``` from a callback, closure, method, etc. I know this is not teaching a man how to fish, but I don't aspire to do that. – ffflabs Jun 18 '14 at 17:06
  • 2
    @amenadiel: Then you can vote to close as duplicate of http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback – Felix Kling Jun 18 '14 at 17:07