2

How can I declare a private var inside a literal object? Becasuse I've this code:

var foo = {

    self: null,

    init: function() {
        self = this;
        self.doStuff();
    },

    doStuff: function() {
        //stuff here
    }

}

This works perfectly, but if I have some objects, the var "self" it will override.. apparently it's a global var.. and I cannot use the restricted word "var" inside this object..

How can I solve this, or make an NameSpace for each object?

Thanks!

mauriblint
  • 1,802
  • 2
  • 29
  • 46
  • 2
    Why `self = this; self.doStuff()` instead of `this.doStuff()`? –  Dec 06 '11 at 15:40
  • -1 Post what you're *actually trying to do* in the question. – RightSaidFred Dec 06 '11 at 16:09
  • possible duplicate of [How to add private variable to this Javascript object literal snippet?](http://stackoverflow.com/questions/1396294/how-to-add-private-variable-to-this-javascript-object-literal-snippet) – naXa stands with Ukraine Jul 28 '15 at 09:02
  • @delnan You would need to do this if you were using the Google Closure Compiler where assigning stuff to "this" is dangerous. – Johann Jul 12 '17 at 06:54

3 Answers3

6

You can create a function scope to hide the variable:

var foo = (function() {
  var self = null
  return {
    init: ...,
    doStuff: ...
  };
})();

Though it is not clear what self is supposed to do here, and how foo is used.

James Clark
  • 1,765
  • 13
  • 17
  • This prevents you from reusing the object literal for more instances. You've effectively created a singleton. – Johann Jul 12 '17 at 06:52
0

You have to use this:

init: function() {
  this.self = this;
  this.self.doStuff();
},

edit However, it's still a property of the "foo" object, and it's not super-clear where you're getting instances of "foo" from. In other words, the way your code is written, there's only one object.

Alternatively, you could create your object with a closure:

var foo = function() {
    var self = null;

    return {
      init: function() {
        self = this;
        self.doStuff();
      },

      doStuff: function() {
        //stuff here
      }
    };
}();
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Not a global variable, per se, but it is accessible from the global scope. – James Clark Dec 06 '11 at 15:37
  • Ah, well sort-of. I see what you mean. I'll update the answer. – Pointy Dec 06 '11 at 15:37
  • The problem is when I've inside a method like init() a jQuery function.. $(selector).click(function(){ self.objectMethod() }) If I haven't a reference to my object, it's impossible call to the objet methods.. – mauriblint Dec 06 '11 at 15:43
  • @mauriblint: Why on earth don't you just include all relevant information in the question?! You have some in the question, some here, and we're still missing some parts, like the relationship between the object and the element clicked. – RightSaidFred Dec 06 '11 at 16:09
0

You are not even using the property that you have created. Instead you create another global variable with the same name. Use the this keyword to access properties:

var foo = {

  self: null,

  init: function() {
    this.self = this;
    this.self.doStuff();
  },

  doStuff: function() {
    //stuff here
  }

}

(Although saving this in a property is truly pointless...)

If you want a local variable in the object, create a closure for it:

var foo = (function(){

  var self = null;

  return {

    init: function() {
      self = this;
      self.doStuff();
    },

    doStuff: function() {
      //stuff here
    }
  };

}());
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • The problem is when I've inside a method like init() a jQuery function.. $(selector).click(function(){ self.objectMethod() }) If I haven't a reference to my object, it's impossible call to the objet methods.. Have I made myself clear? Thanks! – mauriblint Dec 06 '11 at 15:51
  • 1
    @mauriblint: That's because you are not *inside* the `init` method when the event happens. Use a closure as I showed above. – Guffa Dec 06 '11 at 16:27