Created
September 22, 2011 19:03
-
-
Save travisperson/1235688 to your computer and use it in GitHub Desktop.
Overwriting a function prototype
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 util = require("util"); | |
//// Model //// | |
var Model = function(data) { | |
this._data = data | |
} | |
Model.prototype.show = function (data, cb) { | |
// act on data and then return exicute the callback | |
// like pulling something from the database | |
cb(this._data[data.user]) | |
} | |
/// Collections Model | |
var collectionModel = function (data) { | |
Model.call(this, data) | |
} | |
util.inherits(collectionModel, Model) | |
/// Extending the model | |
collectionModel.prototype.show = function (data, cb) { | |
var _show = this.show | |
_show(data, function (data) { // TypeError: undefined is not a function | |
data.name = data.name.toUpperCase(); | |
cb(data); | |
}) | |
} | |
var data = [ | |
{ | |
name: 'Travis' | |
} | |
, { | |
name: 'John' | |
} | |
, { | |
name: 'Sarah' | |
} | |
] | |
// | |
// General model | |
// | |
model = new Model(data) | |
model.show({ | |
user: 1 | |
}, function (data) { | |
console.dir(data) | |
}) | |
// | |
// Collection model, returns name in uppercase | |
// | |
collection = new collectionModel(data) | |
collection.show({ | |
user: 1 | |
}, function (data) { | |
console.dir(data) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment