Skip to content

Commit

Permalink
created a few best practice examples
Browse files Browse the repository at this point in the history
  • Loading branch information
williamli committed May 26, 2015
1 parent 497b5bb commit c3862b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ Private instance functions defrined by using `var` in Object declaration.
```javascript
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*.
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_private_function`.
Create pubilc functions with `prototype` like `best_practice_public_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.0',
version: '1.0.1',
// 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
11 changes: 11 additions & 0 deletions scope-permission-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ ScopePermissionDemo = function(aString) {

}


_best_practice_private_function = function() {
console.log('this is the best way to make a private function accessible by prototype functions');
}

_best_practice_getter_for_private_variable = function() {
return _private_variable;
}

self.instance_function_by_self = function() {
console.log('this is a public instance function created by "self"');
Expand Down Expand Up @@ -128,6 +135,10 @@ ScopePermissionDemo.prototype.instance_function_by_prototype = function() {
}
}

ScopePermissionDemo.prototype.best_practice_public_function = function() {
console.log('access to _private variable by calling a private getter:',_best_practice_getter_for_private_variable());
}

ScopePermissionDemo.static_function = function() {

console.log('this is a public static function');
Expand Down

0 comments on commit c3862b5

Please sign in to comment.