Welcome to the Linux Foundation Forum!

Mistake: Lab 5.2 - Prototypal Inheritance

Options
dmsheiko
dmsheiko Posts: 16
edited October 2023 in LFW211 Class Forum

Hi,

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

assert(Object.getOwnPropertyNames(felixProto).length, 1)
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1)
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1)
assert(typeof felixProto.meow, 'function')
assert(typeof felixProtoProto.purr, 'function')
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:

// prototype checks, do not remove
const felixProto = Object.getPrototypeOf(felix)
const felixProtoProto = Object.getPrototypeOf(felixProto)
const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto)

function checkOwnPropertyNames(proto, right, ...wrongs) {
    const ownPropertyNames = Object.getOwnPropertyNames(proto)
    assert(ownPropertyNames.includes(right), `${ownPropertyNames} should contain ${right}`)
    for (const wrong of wrongs) {
        assert(!ownPropertyNames.includes(wrong), `${ownPropertyNames} should NOT contain ${wrong}`)
    }
}
checkOwnPropertyNames(felixProto, 'meow', 'purr', 'hiss')
checkOwnPropertyNames(felixProtoProto, 'purr', 'hiss', 'meow')
checkOwnPropertyNames(felixProtoProtoProto, 'hiss', 'meow', 'purr')

assert.equal(typeof felixProto.meow, 'function')
assert.equal(typeof felixProtoProto.purr, 'function')
assert.equal(typeof felixProtoProtoProto.hiss, 'function')
console.log('prototype checks passed!')

Regards,
Dmytro

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

Comments

  • xdxmxc
    xdxmxc Posts: 148
    Options

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

    const assert = require('assert')
    
    // TODO: 
    // implement a way to create a prototype chain
    // of leopard -> lynx -> cat
    // leopard prototype must have ONLY a hiss method
    // lynx prototype must have ONLY a purr method
    // cat prototype must have ONLY a meow method
    
    const felix = null //TODO replace null with instantiation of a cat
    felix.meow() // prints Felix the cat: meow
    felix.purr() // prints Felix the cat: prrr
    felix.hiss() // prints Felix the cat: hsss
    
    // prototype checks, do not remove
    const felixProto = Object.getPrototypeOf(felix)
    const felixProtoProto = Object.getPrototypeOf(felixProto)
    const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto)
    const protoPropNames = Object.getOwnPropertyNames(felixProto)
    const protoProtoPropNames = Object.getOwnPropertyNames(felixProtoProto)
    const protoProtoProtoPropNames = Object.getOwnPropertyNames(felixProtoProtoProto)
    
    assert(protoPropNames.includes('meow'))
    assert(protoProtoPropNames.includes('purr'))
    assert(protoProtoProtoPropNames.includes('hiss'))
    assert(protoPropNames.length <= (protoPropNames.includes('constructor') ? 2 : 1))
    assert(protoProtoPropNames.length <= (protoProtoPropNames.includes('constructor') ? 2 : 1))
    assert(protoProtoProtoPropNames.length <= (protoProtoProtoPropNames.includes('constructor') ? 2 : 1))
    assert.equal(typeof felixProto.meow, 'function')
    assert.equal(typeof felixProtoProto.purr, 'function')
    assert.equal(typeof felixProtoProtoProto.hiss, 'function')
    console.log('prototype checks passed!')
    
    

    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!

Categories

Upcoming Training