Javascript prototype - object.create issue -
I am trying to learn about object.create and prototype inheritance and the following is:
employee = {'attributes': {}, getAttributes: function () {this.attributes return; }, 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 ()); This works because I hope it should be and throws errors:
console.log (OfficeEmployeeInstance.attributes);
is undefined
and
console.log (OfficeEmployeeInstance.getAttributes ());
returns the error:
Uncort type error: The method of undefined tester.js can not call 'hasOwnProperty': 39 Staff: addAttribute tester.js : 39 (anonymous function)
What am I doing wrong here?
The second argument of
Object.create should be a property object. It is an object with a defined structure and specific properties:
var OfficeEmployeeInstance = Object.create (OfficeEmployee, {'attributes': {value: {'id': 123, 'name' : 'Bob'}, writeable: true, countable: true}});
You can get the supported properties.
Comments
Post a Comment