admin管理员组

文章数量:1405871

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!

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!

Share asked Dec 6, 2011 at 15:32 mauriblintmauriblint 1,7072 gold badges29 silver badges47 bronze badges 4
  • 2 Why self = this; self.doStuff() instead of this.doStuff()? – user395760 Commented Dec 6, 2011 at 15:40
  • -1 Post what you're actually trying to do in the question. – RightSaidFred Commented Dec 6, 2011 at 16:09
  • possible duplicate of How to add private variable to this Javascript object literal snippet? – naXa stands with Ukraine Commented Jul 28, 2015 at 9:02
  • @delnan You would need to do this if you were using the Google Closure Compiler where assigning stuff to "this" is dangerous. – Johann Commented Jul 12, 2017 at 6:54
Add a ment  | 

3 Answers 3

Reset to default 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.

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
      }
    };
}();

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
    }
  };

}());

本文标签: Private var inside Javascript literal objectStack Overflow