codingdir logo sitemap sitemap |

Javascript accessing object variable from another callback without placing variable in global scope

By : , Category : javascript

Use a closure:

var module = (function () {
    var b; //Scoped to this module

    return { //Return object with methods
        load: function () {
            b = 25; //This refers to the module's b
        add: function () {
            console.log('The value of b is '+ b);
})(); //Self invoking function, invokes instantly.

module.load(); //b is now 25.
module.add(); //The value of b is 25
console.log(b); //undefined, out of scope.

Now all the "private" variables are scoped directly to the module, and don't affect global scope.

ReLated :

The value of this inside a function depends on how that function is called. If you want a function to always be called with a certain value as this, the simplest way is to use Function.prototype.bind:

    index: function(){
        return function(text, render) {
            return this.idx;
        }.bind(this); // <--- HERE

In this example i relied on the fact that the outer function will be called like this:


So inside it, this will actually be MessageHelper. Thus, MessageHelper will be this inside the function returned by index as well.

If you plan to do stuff like pass the index function as parameter somewhere, it won't be called as expected and the whole thing will fail. In this case you should bind to MessageHelper:

var MessageHelper = {
  index : function(){}.bind(MessageHelper);

var myIndex = MessageHelper.index;
myIndex(); // this will return the correct function, bound to MessageHelper

You're passing eg as a parameter to second. It might be easier to see if you renamed it:

second: function(x) {

It gets modified in the first function because you modify it with the second function.

The this in your event and .each() callback functions isn't the same this where you defined x and socket. When those callback functions are invoked by the library and jQuery (respectively), the value of this within those callback functions is usually being provided.

To solve this, your listen function should maybe look more like:

listen: function() {
 var that = this; // <-- keep a reference to your module
 alert(that.x + " " + that.socket);           // fine get somevalue & [object, object]
 that.socket.on('event', function(data) {
    // in here, this could vary depending on the event
    alert(that.x + " " + that.socket);        // this.x undefined this.socket [object, object]
    $.each(data, function(index,value) { 
       // in here, this = the current data element
       alert(that.x + " " + that.socket);     // undefined undefined  

By using var, you're telling the engine to use a local variable named foo, shadowing the global one.

The reason you get undefined on the alert, is that using var affects the whole scope, not just from that point onwards. You could've written:

var foo;
foo = "Value has been modified";

Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .