-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope-permission-demo.js
185 lines (141 loc) · 5.56 KB
/
scope-permission-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// exported
var global_shared_var = "";
ScopePermissionDemo = function(aString) {
var self = this;
// self._simpleSchema = simpleSchema;
self._public_variable = aString;
var _private_variable = aString;
global_shared_unvar = aString;
global_shared_var = aString;
_private_function_by_unvar_function = function() {
console.log('this is a private function created by unvar function (shared globally)');
console.log('shared "_private_variable":',_private_variable);
}
var _private_function_by_var_function = function() {
console.log('this is a private function created by var function (one function object per instance)');
console.log('_private_variable:',_private_variable);
}
_best_practice_private_function = function() {
console.log('this is the best way to make a private function accessible by prototype functions');
}
self._best_practice_getter_for_private_variable = function() {
// do something...
return _private_variable;
}
self._best_practice_setter_for_private_variable = function(new_private) {
// do something ...
_private_variable = new_private;
}
self.instance_function_by_self = function() {
console.log('this is a public instance function created by "self"');
var self = this;
console.log('global_shared_var:',global_shared_var);
console.log('global_shared_unvar:',global_shared_unvar);
console.log('self._public_variable:', self._public_variable);
try {
console.log('_private_variable:', _private_variable);
} catch (e) {
console.error('_private_variable failed');
console.error(e);
}
try {
console.log('calling _private_function_by_var_function...');
_private_function_by_var_function();
} catch (e) {
console.error('calling _private_function_by_var_function failed');
console.error(e);
}
try {
console.log('calling _private_function_by_unvar_function...');
_private_function_by_unvar_function();
} catch (e) {
console.error('calling _private_function_by_unvar_function failed');
console.error(e);
}
}
self.best_practice_public_function_self = function() {
var self = this;
console.log('access to _private_variable directly:', _private_variable);
console.log('updating _private_variable to new vaule directly...');
_private_variable = _private_variable+" updated!";
console.log('_private_variable:',_private_variable);
}
// this.instance_function_by_this = function() {
// console.log('this is a public instance function created by "this"');
// var self = this;
// console.log('global_shared_var:',global_shared_var);
// console.log('global_shared_unvar:',global_shared_unvar);
// console.log('self._public_variable:', self._public_variable);
// try {
// console.log('_private_variable:', _private_variable);
// } catch (e) {
// console.error('_private_variable failed');
// console.error(e);
// }
// try {
// console.log('calling _private_function_by_var_function...');
// _private_function_by_var_function();
// } catch (e) {
// console.error('calling _private_function_by_var_function failed');
// console.error(e);
// }
// try {
// console.log('calling _private_function_by_unvar_function...');
// _private_function_by_unvar_function();
// } catch (e) {
// console.error('calling _private_function_by_unvar_function failed');
// console.error(e);
// }
// }
}
ScopePermissionDemo.prototype.instance_function_by_prototype = function() {
console.log('this is a public instance function created by prototype');
var self = this;
console.log('global_shared_var:',global_shared_var);
console.log('global_shared_unvar:',global_shared_unvar);
console.log('self._public_variable:', self._public_variable);
try {
console.log('_private_variable:', _private_variable);
} catch (e) {
console.error('_private_variable failed');
console.error(e);
}
try {
console.log('calling _private_function_by_var_function...');
_private_function_by_var_function();
} catch (e) {
console.error('calling _private_function_by_var_function failed');
console.error(e);
}
try {
console.log('calling _private_function_by_unvar_function...');
_private_function_by_unvar_function();
} catch (e) {
console.error('calling _private_function_by_unvar_function failed');
console.error(e);
}
console.log('calling self.instance_function_by_self...');
self.instance_function_by_self();
}
ScopePermissionDemo.prototype.best_practice_public_function_prototype = function() {
var self = this;
var current_private = self._best_practice_getter_for_private_variable();
console.log('access to _private_variable by calling a private getter:',current_private);
console.log('updating _private_variable to new vaule with _best_practice_setter_for_private_variable...');
self._best_practice_setter_for_private_variable(current_private+" updated!");
console.log('_private_variable:',self._best_practice_getter_for_private_variable());
}
ScopePermissionDemo.static_function = function() {
console.log('this is a public static function');
}
ScopePermissionDemo.test = function() {
console.log('creating 2 variables for demostration...');
x = new ScopePermissionDemo('this is x');
y = new ScopePermissionDemo('this is y');
console.log('lets look at x:');
x.instance_function_by_prototype();
x.instance_function_by_self();
console.log('lets look at y:');
y.instance_function_by_prototype();
y.instance_function_by_self();
}