Last active
July 16, 2016 03:48
-
-
Save kevincennis/b07b8465f805fcad41b4 to your computer and use it in GitHub Desktop.
Class.js (Simple inheritance setup)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function construct() { | |
if ( this && typeof this.init === 'function' ) { | |
this.init.apply( this, arguments ); | |
} | |
} | |
function extend( obj ) { | |
var proto = Object.create( this && this.prototype || {} ), | |
type = typeof obj, | |
key; | |
obj = type === 'function' ? obj.prototype : type === 'object' ? obj : {}; | |
function Class() { | |
return this.constructor.apply( this, arguments ); | |
} | |
for ( key in obj ) { | |
if ( obj.hasOwnProperty( key ) ) { | |
proto[ key ] = obj[ key ]; | |
} | |
} | |
Class.prototype = proto; | |
proto.constructor = proto.constructor || construct; | |
Class.extend = extend; | |
return Class; | |
} | |
module.exports = extend.call( Object, { constructor: construct } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Class = require('./Class'); | |
var EventEmitter = require('events').EventEmitter; | |
// Base class, inherits from EventEmitter | |
var Base = Class.extend( EventEmitter ); | |
// Model class, adds basic setter/getter methods on top of EventEmitter | |
var Model = Base.extend({ | |
// As long as we inherit the default Class constructor, | |
// this init method will be called on instantiation and passed | |
// whatever arguments were received by the constructor | |
init: function( obj ) { | |
var key; | |
this._attributes = {}; | |
for ( key in obj ) { | |
this.set( key, obj[ key ] ); | |
} | |
}, | |
set: function( key, val ) { | |
this._attributes[ key ] = val; | |
this.emit( 'change', key, val ); | |
}, | |
get: function( key ) { | |
return this._attributes[ key ]; | |
} | |
}); | |
// User class, adds some user-specific methods and attributes on top of Model | |
var User = Model.extend({ | |
avatarBaseURL: 'http://static.somedomain.com/avatars/{{id}}.jpg', | |
getFullName: function() { | |
return this.get('firstName') + ' ' + this.get('lastName'); | |
}, | |
getAvatarURL: function() { | |
return this.avatarBaseURL.replace( /\{\{id\}\}/, this.get('id') ); | |
} | |
}); | |
// Create a User instance with some data | |
var user = new User({ | |
firstName: 'John', | |
lastName: 'Doe', | |
id: 123 | |
}); | |
// Just a little proof that events work | |
user.on( 'change', function( key, val ) { | |
console.log( '%s: %s', key, val ); | |
}); | |
// Set a new name, fire an event | |
user.set( 'firstName', 'Jane' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment