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

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -