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?
    }
}
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68
  • http://stackoverflow.com/questions/7533590/declaring-protected-variable-in-javascript – andypopa Jul 03 '14 at 03:23
  • private and protected are about the same in js, since there are no classes/subclasses/superclasses – dandavis Jul 03 '14 at 03:47
  • @dandavis why do you think so? In js we can make sense of use classes/subclasses/superclasses. – Navin Rauniyar Jul 03 '14 at 03:50
  • this is as close as i could come, but yuck: http://jsfiddle.net/YPSLT/ – dandavis Jul 03 '14 at 04:16
  • @dandavis post it as an answer. – Navin Rauniyar Jul 03 '14 at 04:36
  • @NavinRauniyar: i can't or i would have (duped). it's rough anyway, but you could make the nitty gritty repetitive parts into a callable re-usable routine to give you a sugary way to declare that capability inside the constructor, if you really wanted to use the pattern. personally, i would stick with what's built-in to JS... – dandavis Jul 03 '14 at 04:47

1 Answers1

0

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

TGH
  • 38,769
  • 12
  • 102
  • 135