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 807bfbe

Browse files
protoype privacy, revealing module pattern
1 parent f20cce9 commit 807bfbe

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎05-object-creation.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function myFunction(){
5757
* any variables part of constructor closure are not visible outside of it
5858

5959
```js
60-
function Person(){
60+
function Person(){
6161
var secret = 'hey';
6262
return {
6363
doIt: function (){
@@ -78,3 +78,35 @@ me.doIt(); //does it
7878
* internal arrays/objects are still modifiable via reference
7979
* make sure you return a new object with only the properties needed by caller
8080
* or copy your objects/arrays (with utility methods)
81+
82+
## private properties on prototype
83+
84+
* properties are re-created every time an object is initialized
85+
* shared properties of prototype can also be made private with the same pattern
86+
87+
```js
88+
Person.prototype = (function(){
89+
//all person sharing the same secret
90+
var secret = 'hey';
91+
return {
92+
doIt: function (){
93+
// can see secret
94+
}
95+
}
96+
}());
97+
```
98+
99+
## revealing module pattern
100+
101+
* revealing private methods by assigning them to properties of the returned object
102+
* can name the property differently to the internal function
103+
* can expose internal function under more than one names
104+
105+
```js
106+
Person.prototype = (function(){
107+
function sayHello() {}
108+
return {
109+
greeting: sayHello
110+
}
111+
}());
112+
```

0 commit comments

Comments
(0)

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