Loïc TRUCHOT
JavaScript Fullstack Expert & Senior web developer
Par Loïc TRUCHOT
var a = 5;
var b = false;
var c = 2 + 3;
var d = function() {
console.log('program scope:', a + c);
} // 1 2
if (a === c) { // 1 3
d(); // 4
} else {
a = b; // 1 2
}
var newProgram = {
a: 5,
b: false,
c: 2 + 8,
d: function() {
console.log('subprogram :', this.a + this.c);
}
}
newProgram.d();
var dog = {
concept: 'animal',
species: 'dog',
paws: 4,
sayIMBeautiful: function () {
console.log("I'm a beautiful " + this.species +
', the cutest ' + this.concept + ' with ' + this.paws + ' paws.');
}
}
var Cat = function () {
}
Cat.prototype = dog;
cat = new Cat();
dog.sayIMBeautiful();
cat.sayIMBeautiful();
var bonjour = {};
console.log(bonjour.constructor)
// PROTOTYPE
var player = {
name: "unknow player",
score: 0,
win: function() {
this.score++;
},
displayScore: function() {
console.log("score of " + this.name + " is " + this.score);
}
};
var peach = Object.create(player);
peach.name = 'Peach';
peach.win();
peach.displayScore();
console.log(peach);
// composition over inheritance
var planet = {
name: 'Mars'
}
console.log('typeof ' + planet.name + ' is ' + typeof planet);
console.log('constructor of ' + planet.name + ' is ' + planet.constructor)
var fish = "killerwhale".replace('k', '');
console.log('typeof ' + fish + ' is ' + typeof fish);
console.log('construct of ' + fish + ' is ' + fish.constructor)
console.log(planet instanceof Object);
console.log(fish instanceof String);
// replace ? toString ?
console.log((2).toString())
console.log('test'.replace('t', ''));
export class Player {
public name;
private _banned;
private _level;
readonly type = 'Human Player';
protected isRich = false;
static levels = {
n: 'noob',
a: 'average',
m: 'master'
};
constructor(name, public age, banned?) {
this.name = name;
this._banned = banned;
}
getStatus() {
return this._banned;
}
get formattedAge() {
return this.age + ' ans';
}
set level(lvl) {
this._level = Player.levels[lvl];
}
get level(): string {
return this._level;
}
}
import { Player } from './Player.class';
export class RichPlayer extends Player {
constructor(name, age, public amount) {
super(name, age, false);
// this._banned = true;
this.isRich = true;
}
getStatus() {
console.log('Les joueurs riches ne peuvent pas être bannis');
return super.getStatus();
}
}
By Loïc TRUCHOT