Created chapter 02
This commit is contained in:
@@ -27,7 +27,9 @@
|
||||
},
|
||||
"extends": ["eslint:recommended", "plugin:flowtype/recommended"],
|
||||
"plugins": ["flowtype"],
|
||||
"rules": {}
|
||||
"rules": {
|
||||
"no-console": "off"
|
||||
}
|
||||
},
|
||||
"prettier": {
|
||||
"tabWidth": 4,
|
||||
|
||||
Generated
+4311
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "simpleproject",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple project to show package.json creation",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "babel src -d out",
|
||||
"buildNode": "flow-remove-types src/ -d out/",
|
||||
"flow": "flow",
|
||||
"eslint": "eslint src",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Federico Kereki",
|
||||
"license": "ISC",
|
||||
"babel": {
|
||||
"presets": ["env", "flow"]
|
||||
},
|
||||
"eslintConfig": {
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2017,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"parser": "babel-eslint",
|
||||
"env": {
|
||||
"browser": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": ["eslint:recommended", "plugin:flowtype/recommended"],
|
||||
"plugins": ["flowtype"],
|
||||
"rules": {
|
||||
"no-console": "off"
|
||||
}
|
||||
},
|
||||
"prettier": {
|
||||
"tabWidth": 4,
|
||||
"printWidth": 75
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-eslint": "^8.2.2",
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"babel-preset-flow": "^6.23.0",
|
||||
"eslint": "^4.19.0",
|
||||
"eslint-config-recommended": "^2.0.0",
|
||||
"eslint-plugin-flowtype": "^2.46.1",
|
||||
"flow-bin": "^0.68.0",
|
||||
"flow-remove-types": "^1.2.3",
|
||||
"prettier": "^1.11.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
class ExtDate extends Date {
|
||||
fullDate() {
|
||||
const months = [
|
||||
"JAN",
|
||||
"FEB",
|
||||
"MAR",
|
||||
"APR",
|
||||
"MAY",
|
||||
"JUN",
|
||||
"JUL",
|
||||
"AUG",
|
||||
"SEP",
|
||||
"OCT",
|
||||
"NOV",
|
||||
"DEC"
|
||||
];
|
||||
|
||||
return (
|
||||
months[this.getMonth()] +
|
||||
" " +
|
||||
String(this.getDate()).padStart(2, "0") +
|
||||
" " +
|
||||
this.getFullYear()
|
||||
);
|
||||
}
|
||||
|
||||
static getMonthName(m) {
|
||||
const months = [
|
||||
"JAN",
|
||||
"FEB",
|
||||
"MAR",
|
||||
"APR",
|
||||
"MAY",
|
||||
"JUN",
|
||||
"JUL",
|
||||
"AUG",
|
||||
"SEP",
|
||||
"OCT",
|
||||
"NOV",
|
||||
"DEC"
|
||||
];
|
||||
return months[m];
|
||||
}
|
||||
|
||||
fullDate2() {
|
||||
return (
|
||||
ExtDate.getMonthName(this.getMonth()) +
|
||||
" " +
|
||||
String(this.getDate()).padStart(2, "0") +
|
||||
" " +
|
||||
this.getFullYear()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(new ExtDate().fullDate()); // "MAY 01 2018"
|
||||
|
||||
console.log(ExtDate.getMonthName(8)); // "SEP"
|
||||
console.log(new ExtDate().fullDate2()); // "MAY 01 2018"
|
||||
@@ -0,0 +1,130 @@
|
||||
class Person {
|
||||
constructor(first, last) {
|
||||
this.first = first;
|
||||
this.last = last;
|
||||
}
|
||||
|
||||
initials() {
|
||||
return `${this.first[0]}${this.last[0]}`;
|
||||
}
|
||||
|
||||
fullName() {
|
||||
return `${this.first} ${this.last}`;
|
||||
}
|
||||
|
||||
get lastFirst() {
|
||||
return `${this.last}, ${this.first}`;
|
||||
}
|
||||
|
||||
set lastFirst(lf) {
|
||||
// very unsafe; no checks!
|
||||
const parts = lf.split(",");
|
||||
this.last = parts[0];
|
||||
this.first = parts[1];
|
||||
}
|
||||
}
|
||||
|
||||
pp = new Person("Erika", "Mustermann");
|
||||
console.log(pp); // Person {first: "Erika", last: "Mustermann"}
|
||||
console.log(pp.initials()); // "EM"
|
||||
console.log(pp.fullName()); // "Erika Mustermann"
|
||||
|
||||
pp.lastFirst = "Svensson, Sven";
|
||||
console.log(pp); // Person {first: " Sven", last: "Svensson"}
|
||||
|
||||
class Developer extends Person {
|
||||
constructor(first, last, language) {
|
||||
super(first, last);
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
fullName() {
|
||||
// redefines the original method
|
||||
return `${super.fullName()}, ${this.language} dev`;
|
||||
}
|
||||
}
|
||||
|
||||
window.dd = new Developer("John", "Doe", "JS");
|
||||
console.log(dd); // Developer {first: "John", last: "Doe", language: "JS"}
|
||||
console.log(dd.initials()); // "JD"
|
||||
console.log(dd.fullName()); // "John Doe, JS dev"
|
||||
|
||||
const toJsonMixin = base =>
|
||||
class extends base {
|
||||
toJson() {
|
||||
return JSON.stringify(this);
|
||||
}
|
||||
};
|
||||
|
||||
const countKeysMixin = base =>
|
||||
class extends base {
|
||||
countKeys() {
|
||||
return Object.keys(this).length;
|
||||
}
|
||||
};
|
||||
|
||||
class PersonWithTwoMixins extends toJsonMixin(countKeysMixin(Person)) {
|
||||
toJson() {
|
||||
// redefine the method, just for the sake of it
|
||||
return "NEW TOJSON " + super.toJson();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
p2m = new PersonWithTwoMixins("Jane", "Roe");
|
||||
console.log(p2m);
|
||||
console.log(p2m.toJson()); // NEW TOJSON {"first":"Jane","last":"Roe"}
|
||||
console.log(p2m.countKeys()); // 2
|
||||
|
||||
|
||||
|
||||
class ExtDate extends Date {
|
||||
fullDate() {
|
||||
const months = [
|
||||
"JAN",
|
||||
"FEB",
|
||||
"MAR",
|
||||
"APR",
|
||||
"MAY",
|
||||
"JUN",
|
||||
"JUL",
|
||||
"AUG",
|
||||
"SEP",
|
||||
"OCT",
|
||||
"NOV",
|
||||
"DEC"
|
||||
];
|
||||
|
||||
return (
|
||||
months[this.getMonth()] +
|
||||
" " +
|
||||
String(this.getDate()).padStart(2, "0") +
|
||||
" " +
|
||||
this.getFullYear()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
static getMonthName(m) {
|
||||
const months = [
|
||||
"JAN",
|
||||
"FEB",
|
||||
// etc...
|
||||
"NOV",
|
||||
"DEC"
|
||||
];
|
||||
return months[m];
|
||||
}
|
||||
|
||||
fullDate2() {
|
||||
return (
|
||||
ExtDate.getMonthName(this.getMonth()) +
|
||||
" " +
|
||||
String(this.getDate()).padStart(2, "0") +
|
||||
" " +
|
||||
this.getFullYear()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(new ExtDate().fullDate()); // "MAY 01 2018"
|
||||
Reference in New Issue
Block a user