7 kyu
JavaScript class-like objects
11,095 of 11,337jhoffner
Loading description...
Object-oriented Programming
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
this kata is outdated since JavaScript has had proper classes. it should either be retired or change every instance of "class-like" to "class"
Error "Response received but no data was written to STDOUT or STDERR."
No sample tests? It is unclear what I am supposed to do here. I'm not keen on your sample test procedures. Going back to leetcode.
no sample tests, not able to understand the error
Already reported below, please don't duplicate issues.
Please add test cases for this
already raised there
The tests don't tell us anything about where the error is.
no sample tests
why the description says "class-like object" and not just "class"? There's some difference? It's a real doubt.
this kata predates the introduction of classes to JavaScript. Nowadays, it would be more idiomatic to use a
class
.templates literals doesn't work, WHY?
Is it because version of Node?
Just submitted a working solution with template literals; the issue is not with the kata.
Template literals do work for me
Maybe you're pressing 'test' button, and those tests are missing for this kata, you should just 'submit'
@marcosantoniodev template literals use backticks, not single quotes.
Yeah, i undestand how templates literals are (e.g.
${variable} some text
).when i used a external prototype it doesnt work, but it can be a typpo of mine.
I have one question, why the description says "class-like object" and not just "class"? There's some difference? It's a real doubt.
This comment has been hidden.
Very thanks by your help and explication, Riley!
Сломал голову пока думал почему тесты не проходят, но случайно нажал Submit и все прошло. Первый раз такое вижу, теплых слов автору...
This comment has been hidden.
Your link is dead !
This comment has been hidden.
I think you fixed your code already, but for the benefit of others, the output dog.toString() needs to change when the name is changed. Your version of toString() returns a string that never changes.
This comment has been hidden.
i receive this error, too: "animal.toString() does not return correct value"
i made tests and it returns exactly what the kata's description say. the test does not provide any description about the error. is it the test wrong?
Seems broken, finally hit the cheat button and pasted successful code and still getting 'Unknown Error'.
I'm stumped by why I received this error on the last test, when I submitted my solution: Test Failed: animal.toString() does not return correct value. I even took my code and ran it in JSFiddle & it ran without any errors.
I'm a puzzled by the "Test Failed: animal.toString() does not return correct value" error even though I have defined the toString method which returns a sentence containing this.type and this.name.
What is actually expected from this method ?
Thanks
It can't be just any sentence containing the type and name. The name and type need to be literally joined by the words "is a".
Thank you!
However, I did not find this information in the rules that were given for this Kata. Where should I have found it ?
It was in a code comment in an example in the description :P The description could have been clearer on that point...
Thanks Iaoris
templates literals doesn't work, but WHY?
Is it because the version of Node?
i wasted too much time by this limitation (backticks doesnt work here)
See reply above, backticks work but you aren't using them.
The explanation doesn't mention that the object properties have to be mutable. I used object.create and had the last test fail because I didn't set the attributes to be writable.
It should explicitly mention that your object should support 'foo.name = "bar";'
Agreed, I'd deliberately made name and type immutable out of habit. The error message claimed that toString was returning the wrong result, took a while to work out why!
Fixed.
If type starts with a vowel then... "Max is a alligator" is correct?
g
Nice kata - always train the basics)
my code for the tostring is correct however it keeps failing the test?? tried multiple different way of setting it, using proto and prototype both inside and outside of the original class...
This comment has been hidden.
Done. Thanks.
does anyone know why i get "Failed to construct test fixture properly" after hitting the "Test" button, but on submit it says i am good to go?
The test button is for writing your own tests in the bottom code section. When you submit, your code is tested against the kata author's tests and not your own.
and it has just been rendered obvious.. thanks
Also I believe you would take far more of a "performance hit" from defining and allocating a new function on every instance constructed than the minuscule "performance hit" of looking up the prototype chain. Not that either of these would be significant in the example but because we are talking about fundamentals for new js coders I'd say the former (allocating new functions) would be "far worse" than the prototype look up.
The two sort of cardinal approaches I see here are, 1: putting the toString method in the constructor itself and 2. Attaching it via the constructor's prototype. I considered both, but did 1 because I know you take a small performance hit when your object has to go climbing up it proto chain to find things... are there any concrete drawbacks to using 1 over 2? One I can think of is that iterating over the object with hasOwnProperty will include the toString, where it might be a bit cleaner to just have name and type... thoughts?
Someone else asked a similar question. See here for my response to that. In short though, JavaScript is a prototypical language and by not using the prototype you are basically short changing yourself.
The drawback that you mentioned is also a good example. Basically the rule I go by is, if its something that should be on every instance of that object (usually a method) than use the prototype. If its something specific to that class (usually a variable/field) than it belongs in the constructor. One example of where you might want to define a method within the constructor is if it references a scoped variable within the constructor. Thats not something that is typically done though (in my experience).