JavaScript for Absolute Beginners 6

In this tutorial, we’re going to implement the tic-tac-toe game using JavaScript.



Other tutorials of this series.

Notes for this tutorial:

Task for this tutorial:

  • Implement the tic-tac-toe game

Why need class/OOP?

  • programs will become more and more difficult to maintain when they grow larger and larger

Class VS Object:

  • Class is the definition of objects
  • Class is a type
  • An object is a real example of the type

Class

Class
1
2
3
4
5
6
7
8
9
10
11
12
class Foo {
constructor() {
this.bar = "hello"; //member variable or property
}

someFunction() { //member function or member method
//...
}
}

const foo = new Foo(); //create an object
foo.someFunction(); //call the member function of the object

2D array

2D array
1
2
const list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
console.log(list[1][2]); //6

Class Inheritance

Class Inheritance
1
2
3
4
5
6
7
class SomeBaseClass {
//...
}

class SomeDerivedClass extends SomeBaseClass {
//...
}

The source code of the program in this tutorial on GitHub.