Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f20cce9

Browse files
private members with closures
1 parent 30c72b1 commit f20cce9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎05-object-creation.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,31 @@ function myFunction(){
5050
* shorter to type module names afterwards
5151
* deps in one place
5252
* some minifiers won't shorten global var names
53+
54+
## private members with closures
55+
56+
* constructor function create a closure
57+
* any variables part of constructor closure are not visible outside of it
58+
59+
```js
60+
function Person(){
61+
var secret = 'hey';
62+
return {
63+
doIt: function (){
64+
// can see secret
65+
}
66+
}
67+
}
68+
69+
var me = new Person();
70+
me.secrect === undefined;
71+
secret === undefined;
72+
me.doIt(); //does it
73+
```
74+
75+
* privileged methods are the ones declared within the constructor
76+
* privileged methods have access to private members
77+
* old versions of Firefox allowed access to private scope
78+
* internal arrays/objects are still modifiable via reference
79+
* make sure you return a new object with only the properties needed by caller
80+
* or copy your objects/arrays (with utility methods)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /