Timeline for Private variables in JavaScript and their visibility
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 7, 2014 at 11:28 | comment | added | the_marcelo_r | Here's post about private variables & closures, I hope it helps: marcelorjava.wordpress.com/2014/06/07/… | |
| Jul 19, 2013 at 21:17 | vote | accept | CommunityBot | ||
| Jul 19, 2013 at 21:03 | answer | added | dievardump | timeline score: 4 | |
| Jul 19, 2013 at 21:01 | comment | added | Ian | You seem to be confusing the difference between variables and properties (two very different things). A variable (var secret) is accessible in the scope it is declared (in each Container instance). You set a property (getSecret), by using this, which is in the same scope as secret, so you can access it from there. In the scope where you declare c1 (global, I'm guessing), it doesn't have access to the secret variable. Then you dynamically set a "secret" property on a specific instance with c1.secret = 10;, which is shown by the console output. | |
| Jul 19, 2013 at 21:01 | comment | added | sabof | You might want to google for "javascript closure". The concept needs a little time to get used to. | |
| Jul 19, 2013 at 20:56 | comment | added | Niccolò Campolungo | No. .secret is a property of the object, the other is a private variable due to the scope of the function within it's declared. Remember that functions in JS are treated like objects! | |
| Jul 19, 2013 at 20:54 | history | asked | user504674 | CC BY-SA 3.0 |