6 kyu

Implementing Object.create

Description
Loading description...
Fundamentals
Object-oriented Programming
  • Please sign in or sign up to leave a comment.
  • rsschool_6ce3da4a0ee25392 Avatar

    This comment has been hidden.

  • JohanWiltink Avatar
  • trashy_incel Avatar

    There are no tests with functions as prototype. So solutions which just check that typeof proto === 'object' pass.

  • trashy_incel Avatar

    In the Information section, I think, there is some confusion between literals and primitives. A String literal like 'abc' is a primitive, and cannot be passed to Object.create(). But this is also true of any string variable, i.e. any variable whose typeof returns 'string': let str = String(); is also a string primitive and similarly cannot be used as a Prototype, despite the absence of a string literal in its definition.

  • trashy_incel Avatar

    To access inner [[Prototype]] property of object use its __proto__ property

    In modern JavaScript, __proto__ is deprecated, and programmers are encouraged to use Object.set/getPrototypeOf() instead

  • dfhwze Avatar

    This kata doesn't work with Node v10.x. Everything is either null or undefined.

  • PetrSr Avatar

    This note in the kata description is incorrect:

    But NaN, Infinity, /regular expression literals/, function(){} — they all are actually objects.

    NaN and Infinity are numbers and therefore primitive values. They cannot be passed as the prototype into the native Object.create().

  • Voile Avatar

    Approved

    I also fixed a test case where it would fail on Node 6 + Babel version.

  • acraileanu Avatar

    Trying to submit the code throws "Object # has no method 'log'" error.

  • Clarity Avatar

    When submitting my solution I get the error below. I have tested the solution in jsfiddle and it worked fine. Any ideas what's wrong?

     /runner/frameworks/javascript/cw-2.js:264 console.log("" + this.format(_message(Test.trace(ex))
     ^ TypeError: Object # has no method 'log' 
     at (/runner/frameworks/javascript/cw-2.js:264:25) 
     at Object.Test.handleError (/runner/frameworks/javascript/cw-2.js:254:26) 
     at [eval]:3:6 
     at Object. ([eval]-wrapper:6:22)
     at 
     at evalScript (node.js:536:25) 
     at startup (node.js:80:7) 
     at node.js:906:3      
     
     
     
     
    
     
     
     
         
         
    
  • sshmru Avatar

    default tests are confusing

  • anton-gorbikov Avatar
    1. One of predefined tests is not correct - it will always throw an exeption, because citizen is an object, but instanceof is working with functions:
    Test.expect(veteran instanceof citizen, "veteran must inherit citizen");
    
    1. I don't think that ask developers to use proto field is a good idea. Without native Object.create(null) there is no way (that will work in all browsers) to create an object with [[prototype]] === null.
  • jacobb Avatar

    In the example test fixture, what you currently have (as follows) is invalid code:

    var veteran = Object.create(citizen, {panic: function(){
      return "SNAFU";
    });
    

    Try:

    var veteran = Object.create(citizen, {panic: function(){
      return "SNAFU";
    }});
    
  • wthit56 Avatar

    Hrm... it works fine. But you pretty much told us exactly how to implement it. Almost line-by-line. Kinda takes all the fun and challenge out of it, I feel.

  • moorsiek Avatar

    If anybody used a code snippet from the kata's description and had troubles with passing tests based on it — my big apologies!! There was a stupid bug, my fault. It could confuse warriors because gave wrong results, when the solution is actually ok. I've updated it and now it's ok! I'm the evil, undoubtedly! ]:-> And my big thanks to trymedo and westvud for discovering that thing! Well done guys! :)

  • moorsiek Avatar

    This is my first kata, so there could be some unexploded ordnance in the kata. Any feedback is welcome :)