Skip to content

Instantly share code, notes, and snippets.

@quoidautre
Created August 9, 2017 10:47
Show Gist options
  • Save quoidautre/005a482c379f58763a25cf9e28c3aec1 to your computer and use it in GitHub Desktop.
Save quoidautre/005a482c379f58763a25cf9e28c3aec1 to your computer and use it in GitHub Desktop.
OPP javascript
console.clear();
var Person = function() {};
Person.prototype.initialize = function(name, age)
{
this.name = name;
this.age = age;
}
// TODO: create the class Teacher and a method teach
var Teacher = function() {};
Teacher.prototype = new Person();
Teacher.prototype.teach = function(subject) {
return this.name + " is now teaching " + subject;
};
var him = new Teacher();
him.initialize("Adam", 45);
console.log(him.teach("Inheritance"));
-------------------------------------------------------------------------------------
var myPerson = {
age : "",
name : "",
initialize : function(name,age)
{
this.name = name;
this.age = age;
},
display : function() {
console.log(this.name + " has " + this.age);
}
}
myPerson.initialize("fabrice",25);
console.log(myPerson.display());
var Teachers = Object.create(myPerson);
Teachers.initialize("james",56);
console.log(Teachers.display());
-------------------------------------------------------------------------------------
class Personne {
constructor(name) {
this.name = name;
}
getInfo() {
return this.name;
}
}
class Studente extends Personne {
constructor(name, matricule) {
super(name);
this.matricule = matricule;
}
getInfo() {
return super.getInfo() + ' (matricule : ' + this.matricule + ')';
}
}
var student = new Studente('Fred', '1934743');
console.log(student.getInfo());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment