0

I am from c++ background and I am learning javaScript now. I having trouble understanding polymorphism from javScript's perspective. I understand what is polymorphism how is it implemented i c++ and how it is implemented in javaScript

Polymorphism : Objects of different types responding to method calls at run time, where user is not aware of the type of the object.

Implementation in c++ : We create a base pointer and assign derived class's object to it and then we make a function call using that pointer.:

Implementation in javaScript : we create a function in main function object then we create another function with same name in a function object inherited from the main function object and add them in an array and then execute that array

My doubt is in javaScript we are not making the call based on the parent object. I mean like we have base pointer in c++ and using that we are making the specific function call. I am not sure if I could make myself clear , but would be really thankful if someone could help me

yepMe
  • 23
  • 5

1 Answers1

0

C++ is "class based" OOP, while JS is "prototype based" OOP. Moreover, JS is a functional programming language (ref: comment of @Cheers and hth.)

That is why they are different. When you have knowledge of prototype based OOP, you will understand

There is a book called "Secrets of the JavaScript Ninja", I learn JS a lot from that.

Khoa
  • 2,632
  • 18
  • 13
  • 1
    "JS is a functional programming language", no, it's not. – Cheers and hth. - Alf Sep 09 '16 at 06:42
  • I'm not sure what you're trying to express. A functional language is based on the idea of immutable state. JavaScript (and C++) is based on mutating state, that is, assignments. You can do functional programming or at least functional style programming in both JavaScript and C++ but JavaScript supports that better. I find some dicussion of funcional programming in JavaScript in Wikipedia, at (https://en.wikipedia.org/wiki/Functional_programming#Functional_programming_in_non-functional_languages). – Cheers and hth. - Alf Sep 09 '16 at 06:47
  • Thanks, may I update my post with reference to your comment. I think your comment explains the "functional stuff" better than me – Khoa Sep 09 '16 at 06:50
  • Of course, but I'm still not sure what you were trying to say. :)= – Cheers and hth. - Alf Sep 09 '16 at 06:51
  • None of the comments clarified my doubt – yepMe Sep 09 '16 at 07:00
  • @yepMe, sorry about this. However, what I can say is you must have knowledge about "protoype based OOP" + "functional programming style of JS". I myself came to JS with Cpp background. It was hard time. Currently, your doubt is hard to clarify because you are trying to explain a concept by another concept. This type of knowledge mapping does not help. – Khoa Sep 09 '16 at 07:15