Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How come Java doesn't need 'prototype' like JS does?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand every major OOP principle in Java, so I thought that learning JS would be piece of cake. But then I came across term called prototype in JS which troubles me. Can someone tell me how Java doesn't need prototypes unlike JS? What are prototypes replaced by in Java and how can I picture it from Java's perspective so I can understand it better in JS?
 
Marshal
Posts: 76120
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wouldn't use English skills as a guide to learning Japanese, You would learn Japanese anew. Similarly, forget all about Java® when learning JS; the similarlity between the names is misleading.
Even the name JS is not quite correct; nobody ever uses the correct name for the language, which is ECMAScript. You haven't heard that name because nobody says ECMAScript.
 
author & internet detective
Posts: 41185
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has type safety and doesn't let you add methods at runtime so prototype doesn't apply. You aren't going to be able to understand it from a Java perspective.
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript's prototype is its means to effect inheritance. Java has classes. It's like asking why a steam engine doesn't need gasoline - different mechanisms to achieve a similar goal.
'
"But wait!" you say "JavaScript now has classes!" Well, sort of -- it's all just syntactic sugar on top of the `prototype` mechanisms.

 
Bear Bibeault
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to Campbell's point: yes, knowing Java is a detriment towards learning JavaScript properly -- especially modern JS.
 
Campbell Ritchie
Marshal
Posts: 76120
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:. . . why a steam engine doesn't need gasoline . . .

Nice to see you again, Bear
Nice analogy. Can I be naughty and change it to, “why a steam engine doesn't have spark plugs.” Or even, “why a petrol (=gasoline) engine doesn't have a boiler.”
 
reply
    Bookmark Topic Watch Topic
  • New Topic