0

In php we can define variables mainly in three ways:

  1. private
  2. protected
  3. public

In javascript we can define variables like this:

function myfunc(){
    var x = 'private'; // private variable
    this.x = 'public'; // public variable
}

Now, I want to know how can I maintain protected variable in javascript?


Is this an answer?

function myfunc(){
    var x = { //private variable
        protectedVariable = 'protected'; //protected variable?
    }
}
6
  • stackoverflow.com/questions/7533590/… Commented Jul 3, 2014 at 3:23
  • private and protected are about the same in js, since there are no classes/subclasses/superclasses Commented Jul 3, 2014 at 3:47
  • @dandavis why do you think so? In js we can make sense of use classes/subclasses/superclasses. Commented Jul 3, 2014 at 3:50
  • this is as close as i could come, but yuck: jsfiddle.net/YPSLT Commented Jul 3, 2014 at 4:16
  • @dandavis post it as an answer. Commented Jul 3, 2014 at 4:36

1 Answer 1

0

You can define private scope in JavaScript using closures, but the language doesn't support granular scope identifiers like PHP does.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.