Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Alex's one day</title>
</head>

<body>
<h4>Hey friend,</h4>
<p>Please open the browser's console to see the result!</p>
<script src="./main.js"></script>
</body>

</html>
76 changes: 76 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Diary {
constructor(ctx) {
const context = {
name: 'Hero',
age: 16,
sexy: 'MALE',
location: 'Beijing',
date: Date.now(),
actions: []
}

Object.assign(context, ctx)
Object.keys(context).map(key => {
this[key] = context[key]
})
}

init() {
this.diaryTitle()
this.eat('Eat Breakfast at', '7:00')
this.workout('Do fitness at', '8:00')
this.reading('Reading Next.js official website at', '8:00')
this.travel('Travel at Weekend')
this.execActions()
}

diaryTitle() {
window.console.log(
'%c%s',
'padding:0 10px; color: #4688f1; background: #fff; font-size: 18px;',
this.name + '\'s diary'
)
}

eat(food, time) {
this._print(food, time)
}

reading(book, time) {
this._print(book, time)
}

workout(content, time) {
this._print(content, time)
}

coding(code, time) {
this._print(code, time)
}

travel(destination, time) {
this._print(destination, time)
}

execActions() {
if (!this.actions.length) return
this.actions.map(action => {
Object.prototype.toString.call(action) === 'object Function' ? action() : null
})
}

_print(content, time) {
if (!content) return
console.log(content, time && time instanceof Date ? new Date(time) : time ? time : '');
}

}

const context = {
name: 'Alex',
age: 18,
sexy: 'FEMALE',
location: 'Beijing'
}
const myDiary = new Diary(context)
myDiary.init()