Permalink
Browse files

Rename initialize to constructor and other cleanups.

  • Loading branch information...
1 parent 7934ddc commit e23f7ad6beb1a203e1754d222ae071362b9678da @creationix committed Aug 19, 2010
Showing with 13 additions and 11 deletions.
  1. +1 −2 README.markdown
  2. +8 −6 lib/class.js
  3. +2 −1 lib/hash.js
  4. +1 −1 lib/nstore.js
  5. +1 −1 lib/queue.js
View
@@ -11,8 +11,7 @@ Creating a database is easy, you just call the nStore function to generate a col
// Load the library
var nStore = require('nstore');
// Create a store
- var users = Object.create(nStore);
- users.initialize('data/users.db', function () {
+ var users = nStore.new('data/users.db', function () {
// It's loaded now
});
View
@@ -3,17 +3,19 @@
var Class = module.exports = Object.create(Object.prototype, {
// Implement extend for easy prototypal inheritance
extend: {value: function extend(obj) {
+ if (obj === undefined) return Object.create(this);
obj.__proto__ = this;
- // Lock the object to be used only as a prototype
- Object.freeze(obj);
+ Object.freeze(obj); // Lock the prototype to enforce no changes
return obj;
}},
+
// Implement new for easy self-initializing objects
- new: {value: function () {
+ new: {value: function new_() {
var obj = Object.create(this);
- if (obj.initialize) obj.initialize.apply(obj, arguments);
- // Lock the object down so the fields are static
- Object.seal(obj);
+ if (typeof obj.constructor === 'function') obj.constructor.apply(obj, arguments);
+ Object.seal(obj); // Lock the object down so the fields are static
return obj;
}}
+
});
+
View
@@ -1,7 +1,8 @@
+
// A Hash is an interable object
var Hash = module.exports = Object.create(Object.prototype, {
// Make for easy converting objects to hashes.
- new: {value: function initialize(value) {
+ new: {value: function (value) {
value = value || {};
value.__proto__ = Hash;
return value;
View
@@ -14,7 +14,7 @@ var nStore = module.exports = Class.extend({
// Set up out local properties on the object
// and load the datafile.
- initialize: function initialize(filename, callback) {
+ constructor: function constructor(filename, callback) {
this.filename = filename;
this.fd = null;
this.index = Hash.new();
View
@@ -3,7 +3,7 @@ var Class = require('./class');
// If a large number of writes gets queued up, the shift call normally
// eats all the CPU. This implementes a faster queue.
var Queue = module.exports = Class.extend({
- initialize: function initialize() {
+ constructor: function constructor() {
if (this === Queue) throw new Error("Cann't use Queue directly");
this.tail = [];
this.head = Array.prototype.slice.call(arguments);

0 comments on commit e23f7ad

Please sign in to comment.