Skip to content

Commit

Permalink
[v1.0.5] prototype methods cannot access private variables! update re…
Browse files Browse the repository at this point in the history
…quired.
  • Loading branch information
williamli committed May 27, 2015
1 parent 3b0dc39 commit 95fa735
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
19degrees:[email protected].3
19degrees:[email protected].5
[email protected]
[email protected]
[email protected]
Expand All @@ -8,7 +8,7 @@ [email protected]
[email protected]
[email protected]
[email protected]
local-test:19degrees:[email protected].3
local-test:19degrees:[email protected].5
[email protected]
[email protected]
[email protected]
Expand Down
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ y = new ScopePermissionDemo('this is y');
##### ScopePermissionDemo.instance_function_by_self
A public instance function created by self.function_name (self.instance_function_by_self) inside the object declaration. It has access to all the _private variables and functions defined (scope controlled by using `var`) inside the object.
##### ScopePermissionDemo.instance_function_by_prototype
A public instance function created by ScopePermissionDemo.prototype (ScopePermissionDemo.prototype.instance_function_by_prototype). It do not have access to any of the _private variables defined inside the object and require globally defined private functions (defined by *not* using `var` in Object declaration) to access the _private variables.
A public instance function created by ScopePermissionDemo.prototype (ScopePermissionDemo.prototype.instance_function_by_prototype). It do not have access to any of the _private variables defined inside the object (with `var`) and require other publicly available functions like `self.instance_function_by_self` to access these variables. When prototype is used, one can consider exposing the varibles in question as `self.public_variable` unless extra setting / getting logic is required.

## Private Instance Functions
##### ScopePermissionDemo._private_function_by_unvar_function
Expand All @@ -31,7 +31,19 @@ var _private_function_by_var_function = function() {...}
```
It can only be access by functions (both public and private) defined in the same Object declaration but not by public functions created using *prototypes*.

### Best Practice
Create private variables using var inside Object declaration like `_private`.
Access private variables via getter and setter like `_best_practice_getter_for_private_variable` and `_best_practice_setter_for_private_variable`.
Create pubilc functions with `prototype` like `best_practice_public_function`.
### Best Practice using Prototypes
- Create private variables using `var` inside Object declaration like `_private`.
- Expose varibles like `self.public_variable`, use `_` in front to "denote it as private" (still accessible publicly) or access private variables via public getters and setters like `self._best_practice_getter_for_private_variable` and `self._best_practice_setter_for_private_variable`.
- Create pubilc functions with `prototype` like `best_practice_public_function_prototype`.

### Best Practice using self.public_function
- Create private variables using `var` inside Object declaration like `_private`.
- Create pubilc functions with `self` like `self.best_practice_public_function_self`.

### Prototype vs SELF / THIS
- SELF / THIS duplicate functions in each instance, prototype allow the same function to only be defined once and shared.
- SELF / THIS allow private variables where as prototype can only access public varibles
- Real access permission vs memory efficency
- As JavaScript is lexically scoped, you can simulate this on a per-object level by using the constructor function as a closure over your 'private members' and defining your methods in the constructor, but this won't work for methods defined in the constructor's prototype property.
- http://thecodeship.com/web-development/methods-within-constructor-vs-prototype-in-javascript/
- http://stackoverflow.com/questions/6784927/how-to-acces-javascript-object-variables-in-prototype-function
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: '19degrees:scope-permission-demo',
version: '1.0.3',
version: '1.0.5',
// Brief, one-line summary of the package.
summary: 'An in-depth look into scoping and permissions of Meteor.',
// URL to the Git repository containing the source code for this package.
Expand Down
33 changes: 24 additions & 9 deletions scope-permission-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ScopePermissionDemo = function(aString) {
_private_function_by_unvar_function = function() {
console.log('this is a private function created by unvar function (shared globally)');

console.log('_private_variable:',_private_variable);
console.log('shared "_private_variable":',_private_variable);
}

var _private_function_by_var_function = function() {
Expand All @@ -29,11 +29,13 @@ ScopePermissionDemo = function(aString) {
console.log('this is the best way to make a private function accessible by prototype functions');
}

_best_practice_getter_for_private_variable = function() {
self._best_practice_getter_for_private_variable = function() {
// do something...
return _private_variable;
}

_best_practice_setter_for_private_variable = function(new_private) {
self._best_practice_setter_for_private_variable = function(new_private) {
// do something ...
_private_variable = new_private;
}

Expand Down Expand Up @@ -70,6 +72,15 @@ ScopePermissionDemo = function(aString) {
}
}

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"');

Expand Down Expand Up @@ -137,14 +148,18 @@ ScopePermissionDemo.prototype.instance_function_by_prototype = function() {
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 = function() {
var current_private = _best_practice_getter_for_private_variable();
console.log('access to _private variable by calling a private getter:',current_private);
console.log('updating _private to new vaule with _best_practice_setter_for_private_variable...');
_best_practice_setter_for_private_variable(current_private+" updated!");
console.log('_private:',_best_practice_getter_for_private_variable());
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() {
Expand Down

0 comments on commit 95fa735

Please sign in to comment.