I am trying to combine an existing object with a class that extends it (and its prototype).
The code is below, I can of course add more detail if needed but I think this covers the problem. As specified amongst the code, the error is TypeError: this.prototype is undefined
. The last line in this snippet is the call which causes this error, the error itself occurs in the .addMixin(mixin) function.
The references I have used to get here include: http://www.joezimjs.com/javascript/javascript-mixins-functional-inheritance/ and http://javascript.crockford.com/prototypal.html (though this was a stepping stone to reach above)
Note: I know this can be done with JQuery and other libraries but I want to do this Vanilla.
// setup some tools
var config = {
writable: true,
enumerable: true,
configurable: true
};
var defineProperty = function(obj, name, value) {
config.value = value;
Object.defineProperty(obj, name, config);
}
// And mixins
Object.prototype.addMixin = function (mixin) {
for (var prop in mixin) {
if (mixin.hasOwnProperty(prop)) {
this.prototype[prop] = mixin[prop]; // this is where the error occurs: 'TypeError: this.prototype is undefined'
}
}
};
// Define File prototype
var File = Object.create(Object.prototype);
defineProperty(File, 'file', null);
defineProperty(File, 'outputID', null);
defineProperty(File, 'hash', null);
defineProperty(File, 'hashThis', function (callback) {});
// define Timestamp prototype
var Timestamp = Object.create(File);
defineProperty(Timestamp, 'protectedHashValue', null);
defineProperty(Timestamp, 'timestamp', null);
defineProperty(Timestamp, 'read', function () {});
defineProperty(Timestamp, 'verify', function () {});
// Later, we take in a file using the HTML5 file API
// First, create the File object (in an array because there are multiple files)
globalStatus.files[fileIndex] = Object.create(File);
// and now place the file in the file property of the new File object
globalStatus.files[fileIndex].file = evt.target.files[fileIndex];
// Later on we determine that a particular file is a timestamp
// We want to mix in the properties of the Timestamp prototype to this existing File object
globalStatus.files[fileIndex].addMixin(Timestamp); // this is the call which initiates the error