1

Hey I am learning JS and trying to write a simple multi-player chess game. I am trying to have all different pieces inherit from the same super class. Like in Java Id have Pawn, Knight, Bishop, etc extend an abstract super class GamePiece. Is that possible?

Or is the closest thing having each type of piece have a separate constructor and reference same functions such as move() or take(), only with different arguments?

jmj
  • 237,923
  • 42
  • 401
  • 438
user1219387
  • 135
  • 1
  • 12
  • http://stackoverflow.com/questions/387707/whats-the-best-way-to-define-a-class-in-javascript/387733#387733 – Barmar Jul 12 '14 at 05:08
  • Try reading anything by John Resig, like his blog. – Barmar Jul 12 '14 at 05:08
  • Using prototypes does seem to be more trouble then benefit. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain on other hand seems to be in the right ball park of what I might do. Thanks! – user1219387 Jul 12 '14 at 05:24
  • JavaScript inheritance, constructor functions and prototype is explained in detail here: http://stackoverflow.com/a/16063711/1641941 no JQuery is needed to do so hope it helps – HMR Jul 12 '14 at 12:50

2 Answers2

2

Java script is class free object oriented programming language. Javascript follows prototypal inheritance instead of classical inheritance to know more about prototypal inheritance visit this.

About abstract class it will difficult to fit Javascript in to any pure object oriented language. To achieve behavior like abstract class visit this stack-overflow question. You will find various ways for same.

Community
  • 1
  • 1
Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
1

It is possible to make something akin to an abstract class using prototypes, but that may be more trouble than is necessary in your case.

Since JavaScript is a dynamically typed language, you don't need to have every piece abstracted from a super class, unlike Java. Separate constructors and methods may work well for you.

(If you'd still prefer an abstract class, read through the linked Stack Overflow article, and comment back if you still need help!)

Community
  • 1
  • 1
username tbd
  • 9,152
  • 1
  • 20
  • 35