Welcome to the Linux Foundation Forum!

Mistake: Lab 5.2 - Prototypal Inheritance

Posts: 28
edited October 2023 in LFW211 Class Forum

Hi,

There are some mistakes in code that verifies the prototype chain.
Here is the code:

  1. assert(Object.getOwnPropertyNames(felixProto).length, 1)
  2. assert(Object.getOwnPropertyNames(felixProtoProto).length, 1)
  3. assert(Object.getOwnPropertyNames(felixProtoProto).length, 1)
  4. assert(typeof felixProto.meow, 'function')
  5. assert(typeof felixProtoProto.purr, 'function')
  6. assert(typeof felixProtoProtoProto.hiss, 'function')
  1. the 3rd line should likely check felixProtoProtoProto rather than felixProtoProto (which is already checked in previous line)
  2. there should be assert.equal rather than assert.
  3. in case of class syntax, Object.getOwnPropertyNames() may return 'constructor' in addition to 'meow', 'purr' or 'hiss', and I believe this is acceptable. So instead of checking the number of property names I would check the content of the array.

So, I would rewrite the checks this way:

  1. // prototype checks, do not remove
  2. const felixProto = Object.getPrototypeOf(felix)
  3. const felixProtoProto = Object.getPrototypeOf(felixProto)
  4. const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto)
  5.  
  6. function checkOwnPropertyNames(proto, right, ...wrongs) {
  7. const ownPropertyNames = Object.getOwnPropertyNames(proto)
  8. assert(ownPropertyNames.includes(right), `${ownPropertyNames} should contain ${right}`)
  9. for (const wrong of wrongs) {
  10. assert(!ownPropertyNames.includes(wrong), `${ownPropertyNames} should NOT contain ${wrong}`)
  11. }
  12. }
  13. checkOwnPropertyNames(felixProto, 'meow', 'purr', 'hiss')
  14. checkOwnPropertyNames(felixProtoProto, 'purr', 'hiss', 'meow')
  15. checkOwnPropertyNames(felixProtoProtoProto, 'hiss', 'meow', 'purr')
  16.  
  17. assert.equal(typeof felixProto.meow, 'function')
  18. assert.equal(typeof felixProtoProto.purr, 'function')
  19. assert.equal(typeof felixProtoProtoProto.hiss, 'function')
  20. console.log('prototype checks passed!')

Regards,
Dmytro

249ymzn0nkk5-LFW211Labs06.28.2023.pdf
h08po53bto1k-labs-june-2023.zip

Comments

  • Posts: 160

    thanks @dmsheiko great catches and feedback, this being fixed, I went with:

    1. const assert = require('assert')
    2.  
    3. // TODO:
    4. // implement a way to create a prototype chain
    5. // of leopard -> lynx -> cat
    6. // leopard prototype must have ONLY a hiss method
    7. // lynx prototype must have ONLY a purr method
    8. // cat prototype must have ONLY a meow method
    9.  
    10. const felix = null //TODO replace null with instantiation of a cat
    11. felix.meow() // prints Felix the cat: meow
    12. felix.purr() // prints Felix the cat: prrr
    13. felix.hiss() // prints Felix the cat: hsss
    14.  
    15. // prototype checks, do not remove
    16. const felixProto = Object.getPrototypeOf(felix)
    17. const felixProtoProto = Object.getPrototypeOf(felixProto)
    18. const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto)
    19. const protoPropNames = Object.getOwnPropertyNames(felixProto)
    20. const protoProtoPropNames = Object.getOwnPropertyNames(felixProtoProto)
    21. const protoProtoProtoPropNames = Object.getOwnPropertyNames(felixProtoProtoProto)
    22.  
    23. assert(protoPropNames.includes('meow'))
    24. assert(protoProtoPropNames.includes('purr'))
    25. assert(protoProtoProtoPropNames.includes('hiss'))
    26. assert(protoPropNames.length <= (protoPropNames.includes('constructor') ? 2 : 1))
    27. assert(protoProtoPropNames.length <= (protoProtoPropNames.includes('constructor') ? 2 : 1))
    28. assert(protoProtoProtoPropNames.length <= (protoProtoProtoPropNames.includes('constructor') ? 2 : 1))
    29. assert.equal(typeof felixProto.meow, 'function')
    30. assert.equal(typeof felixProtoProto.purr, 'function')
    31. assert.equal(typeof felixProtoProtoProto.hiss, 'function')
    32. console.log('prototype checks passed!')
    33.  

    This way failure information is most rich (i.e. first you know if you haven't put a method in a correct place, then you know if there are too many properties anywhere (allowing for constructor), then you know if you've not made it a method. I also prefer flatness and minimum indirection for these scripts. Thanks again!

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training