1

Why does this print out 'bye' instead of 'hello'? According to the inheritance chain as is described in this blog post, I would've thought it would log 'hello'.

http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/

class Test {
  hello() {
    console.log('hello')
  }
}

Test.prototype.hello = function(){
  console.log('bye')
}

const t = new Test
t.hello()
2
  • Why do you overwrite Test.prototype.hello if you don't want that? What else did you expect to happen with the bye-function? Commented May 17, 2017 at 1:35
  • Do you know how class syntax works? What do you think does the inheritance chain of t look like according to the article you read? Commented May 17, 2017 at 1:37

1 Answer 1

3

You are rewriting the definition of hello on the "prototype". When you do class Test () ... hello is the equivalent of

Test.prototype.hello

The class syntax is mostly sugar on top of the normal prototypical definition of a function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.