In my quest to create a flexible "class" in JavaScript, I encountered an issue when trying to assign a property in a prototype as it showed that the prototype was undefined:
Class = {};
Class.extend = function(obj) {
var result = Object.create(this);
if (obj) {
for (var key in obj) {
if(typeof obj[key] == 'function'){
console.log(result);
result.protorype[key] = obj[key];
}else{
result[key] = obj[key];
};
};
result.prototype.constructor = result;
}
return result;
}
var a = Class.extend({
username: "matteo",
password: "nn te la dico",
getByUsername: function() {
return this.username;
}
});
console.log(a, Class.isPrototypeOf(a));
The issue arises specifically when attempting to set the property 'getByUsername' while defining object "a". The console outputs:
Uncaught TypeError: Cannot set property 'getByUsername' of undefined
Meanwhile, the logged "result" only shows the properties "username" and "password".
P.S. This implementation is compatible only with IE versions above 8.
Link to the code: http://jsfiddle.net/paglia_s/z62eA/