Difference between revisions of "Classes"

From rbachwiki
Jump to navigation Jump to search
Line 10: Line 10:
// at a method
// at a method
calculateAge(){
calculateAge(){
  var age = new Date().getFullYear - this.yearOfBirth;
  var age = new Date().getFullYear() - this.yearOfBirth;
  console.log(age);
  console.log(age);
}
}
Line 25: Line 25:
  // call the static method
  // call the static method
  person.greeting();
  person.greeting();
== Inheritance ==
<pre>
class Person{
constructor(name, yearOfBirth, job){
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
// at a method
calculateAge(){
var age = new Date().getFullYear() - this.yearOfBirth;
console.log(age);
}
}
</pre>
''' Create a subclass of person '''
class Athlete6 extends Person {
constructor(name, yearOfBirth,job, olympicGames, medals){
super(name, yearOfBirth, job);
this.oplympicGames = olympicGames;
this.medals = medals;
}
wonMedal(){
this.medals ++;
console.log(this.medals);
}
}
''' Instantiate a new Athlete '''
const johnAthlete = new Athlete('john', 1990, 'swimmer', 3, 10);


==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Revision as of 19:14, 3 December 2016

Classes

class Person{
constructor(name, yearOfBirth, job){
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;

}
// at a method
calculateAge(){
 var age = new Date().getFullYear() - this.yearOfBirth;
 console.log(age);
}
static greeting(){
console.log('hey');
}
}

Create an instance of the class

const john = new Person('john', 1990, 'it');

Static methods are not inherited when you instantiate a new class

// call the static method
person.greeting();

Inheritance

class Person{
constructor(name, yearOfBirth, job){
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;

}
// at a method
calculateAge(){
 var age = new Date().getFullYear() - this.yearOfBirth;
 console.log(age);
}

}

Create a subclass of person

class Athlete6 extends Person {
constructor(name, yearOfBirth,job, olympicGames, medals){
super(name, yearOfBirth, job);
this.oplympicGames = olympicGames;
this.medals = medals;
}
wonMedal(){
this.medals ++;
console.log(this.medals);
}
}

Instantiate a new Athlete

const johnAthlete = new Athlete('john', 1990, 'swimmer', 3, 10);

Back To Top- Home - Category