0

Usually, you can create private variables by using var x instead of this.x in defining function and then you create objects like so: var o = new SomeClass(). However, I need something like PHP class with static methods (object with private variables without any further assigning). Same like defining class and then creating the object from it, but directly defining the object, while still maintaining private variables. Is it possible?


EDIT: Normal private variable definition:

function SomeClass() {
    var x = '1dg5456awd32tr5';
    this.getX = function() {return private;}
}
var o = new SomeClass();
o.getX();

How to save that x variable without using the function?

4
  • Can you be more clear? Try adding some psudo-code to illustrate what you're looking for. Commented May 13, 2014 at 21:19
  • I did not understand the question either. Commented May 13, 2014 at 21:20
  • I think the OP wants to construct an object from an object literal, but with private variables. Closures are the solution, I'll write an answer tomorrow if nobody has by then. Commented May 13, 2014 at 21:24
  • "How to save that x variable without using the function?" You can't, hence it being "private". it's only available within that scope, other than by using said function. Commented May 13, 2014 at 21:24

4 Answers 4

2

I don't know if I have understood correctly your question but you can take advantage of the javascript scope behaviour by creating something like this: http://jsfiddle.net/2R8yY/

var Order = (function(){
    var company = 'ACME';
    var klassOrder = function(){};
    klassOrder.getCompany = function(){
        return company;
    }
    return klassOrder;
})();

console.log(Order.getCompany()); // -> ACME
Sign up to request clarification or add additional context in comments.

Comments

1

Privacy of the local variable scope is not restricted to constructor functions. However, you always need to invoke a function to create a "private" scope - even if that function is not assigned to any identifier.

You seem to be looking for Immediately-Executed Function Expressions or the module pattern.

var o = (function() {
    var x = '1dg5456awd32tr5';
    return {getX: function() {return private;}};
}());
o.getX();

1 Comment

@Ejay: No, OP wants a getter. If you did return x, then you could have written just var o = '1dg5456awd32tr5'; in the first place.
0

Just so you know, in JavaScript functions are objects.

Because JavaScript scope is functional, it becomes easy to restrict member variable access by declaring local variables in a function's scope. Most commonly, the Facade Pattern or Module Pattern are used.

Comments

0

Are you looking for something like this?

var SomeClass = (function(myPrivateThingy) {
  var privateInt = myPrivateThingy;
  return function() {
    this.getInt = function() { return privateInt; }
  }
}());

var instance = new SomeClass(99);
// instance.getInt() === 99

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.