Permalink
Browse files

Inheritable privates

  • Loading branch information...
CrabDude committed Feb 10, 2011
1 parent 370b9b0 commit 2481bb3e132f1f719c8a568614fd8e6a60d93f37

This file was deleted.

Oops, something went wrong.
View
@@ -1,5 +1,8 @@
/*
* Example showing the basic concept behind private variables
* Basic Example showing how to create true private variables
*
* Other examples such as Crockford's, http://www.crockford.com/javascript/private.html,
* initialize functions in the constructor, which is bad for performance.
*
*/
var Person = (function() {
@@ -0,0 +1,9 @@
<html>
<head>
</head>
<body>
<h1>Private.js Example: Simple (Non-Inheritable) Private non-Static Member Scoping</h1>
<script src="../../lib/privates.js"></script>
<script src="basic.js"></script>
</body>
</html>
@@ -0,0 +1,9 @@
<html>
<head>
</head>
<body>
<h1>Private.js Example: Inheritable Private non-Static Member Scoping</h1>
<script src="../../lib/privates.js"></script>
<script src="inheritance.js"></script>
</body>
</html>
@@ -0,0 +1,52 @@
var Person = Class.extend(function(pvtPerson) {
var chromosomes = 48; // private static
return {
init: function(isDancing) {
// console.log("pvtPerson(this).invisible: \r\n >> ", pvtPerson(this).invisible); // => undefined
// console.log("pvtPerson(this).hasSword: \r\n >> ", pvtPerson(this).hasSword); // => undefined
pvtPerson(this).dancing = isDancing;
// console.log("pvtPerson(this).dancing: \r\n >> ", pvtPerson(this).dancing); // => undefined
},
dance: function() {
return pvtPerson(this).dancing;
},
getChromosomeCount: function() {
return chromosomes;
}
};
});
var Ninja = Person.extend(function(pvtNinja) {
return {
init: function(hasSword){
pvtNinja(this).invisible = true;
this._super( false );
pvtNinja(this).hasSword = hasSword || true;
console.log("pvtNinja(this).dancing: \r\n >> ", pvtNinja(this).dancing); // => undefined
},
dance: function(){
// Call the inherited version of dance()
return this._super();
},
swingSword: function(){
return pvtNinja(this).hasSword;
}
};
});
var p = new Person(true);
//console.log("p.dance(): \r\n >> ",p.dance()); // => true
var n = new Ninja();
//console.log("n.dance(): \r\n >> ",n.dance()); // => false
//console.log("n.swingSword(): \r\n >> ",n.swingSword()); // => true
//
//// Should all be true
//console.log("p instanceof Person && p instanceof Class && " +
// "n instanceof Ninja && n instanceof Person && n instanceof Class: \r\n >> ",
// p instanceof Person && p instanceof Class &&
// n instanceof Ninja && n instanceof Person && n instanceof Class);
View

This file was deleted.

Oops, something went wrong.
View
@@ -9,30 +9,46 @@
* Inspired by base2 and Prototype
*/
(function(){
var n=-1, initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
var n=-1, m=-1, privates = [], initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function(){};
function pvt() {
var key = '_pvt'+(++n),
store = [];
function Pvt(_super) {
_super = _super || {};
return function(obj) {
if (typeof obj[key] == 'undefined')
obj[key] = store.push({_self : obj})-1;
// create a store per class declaration
var key = '_pvt'+(++n),
store = [],
pvt = function(obj) {
// initialize a private object for a Class instance
if (typeof obj[key] == 'undefined') {
// extended privates inherits from _super privates
var _superPvt = (typeof privates[_super._pvt] == 'function' && privates[_super._pvt](obj)) || null,
pvtValue = Object.create(_superPvt);
pvtValue._self = obj;
obj[key] = store.push(pvtValue)-1;
}
return store[obj[key]]._self === obj ? store[obj[key]] : null;
};
return store[obj[key]]._self === obj ? store[obj[key]] : null;
};
};
this._pvt = privates.push(pvt)-1;
this.Class.pvt = pvt;
return pvt;
}
// Create a new Class that inherits from this class
Class.extend = function(prop) {
var self = this,
_super = this.prototype;
// if they pass a function generate and pass a new pvt function
if (typeof prop == 'function') {
prop = prop(pvt());
// this === parent class, Class === extended class
prop = prop(Pvt.call(Class,this));
}
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
@@ -67,8 +83,10 @@
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
if ( !initializing && this.init ) {
// create new store
this.init.apply(this, arguments);
}
}
// Populate our constructed prototype object
@@ -79,7 +97,7 @@
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
})();

0 comments on commit 2481bb3

Please sign in to comment.