Permalink
Browse files

updated examples

  • Loading branch information...
CrabDude committed Feb 8, 2011
1 parent 7b6704b commit 370b9b0d7a4780456ed20f647948cc4ce13b9ae5
Showing with 73 additions and 312 deletions.
  1. +0 −9 examples/automatic/index.html
  2. +46 −0 examples/basic/basic.js
  3. +0 −9 examples/manual/index.html
  4. +5 −5 examples/manual/manual.js
  5. +0 −85 index.js
  6. +22 −204 lib/privates.js

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,46 @@
/*
* Example showing the basic concept behind private variables
*
*/
var Person = (function() {
// create a lookup function for easy private retrieval
function pvt(obj) {
if (typeof obj._pkg == 'undefined')
obj._pkg = store.push({_self : obj})-1;
// ensure obj === the original obj and not just an === ._pkg value
return store[obj._pkg]._self === obj ? store[obj._pkg] : null;
}
// create a closure variable to store privates in
var store = []; // private store for instances;
var chromosomes = 48; // private static variable
// create functions in the same prototypical/classical manner
var Self = function(isDancing) {
pvt(this).dancing = isDancing;
};
Self.prototype = {
// access the private variable in a public method
dance: function(){
return pvt(this).dancing;
},
// access the the private static variable in a public method
getChromosomeCount: function() {
return chromosomes;
}
};
return Self;
})();
// Check to see if private variables are in fact private
var p = new Person(true);
p.dance(); // => true
p.getChromosomeCount; // => 48
Person.getChromosomeCount; // => 48
p.dancing; // undefined
p.chromosomes; // undefined

This file was deleted.

Oops, something went wrong.
View
@@ -35,12 +35,12 @@ var Ninja = Person.extend((function() {
var p = new Person(true);
console.log(p,p.dance()); // => true
p.dance(); // => true
var n = new Ninja();
console.log(n,n.dance()); // => false
console.log(n.swingSword()); // => true
n.dance(); // => false
n.swingSword(); // => true
// Should all be true
console.log(p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class);
p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class;
View

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 370b9b0

Please sign in to comment.