Im trying to learn about object.create and prototypal inheritance and have the following:
var Employee = {
'attributes': {},
getAttributes: function() {
return this.attributes;
},
addAttribute: function(attribute) {
if (! this.attributes.hasOwnProperty(attribute)) {
this.attributes.extend(attribute);
}
}
};
var OfficeEmployee = Object.create(Employee);
var OfficeEmployeeInstance = Object.create(OfficeEmployee, {'attributes': {'id': 123, 'name': 'Bob'}});
console.log(OfficeEmployeeInstance.attributes);
OfficeEmployeeInstance.addAttribute({'salary': '100'});
console.log(OfficeEmployeeInstance.getAttributes());
It doesnt work as i expect it should though and throws errors:
console.log(OfficeEmployeeInstance.attributes);
is undefined
and
console.log(OfficeEmployeeInstance.getAttributes());
gives error:
Uncaught TypeError: Cannot call method 'hasOwnProperty' of undefined tester.js:39
Employee.addAttribute tester.js:39
(anonymous function)
What am i doing wrong here?