ES6 Class Playground

by Po-Jung Chen

HTML

<div id="log"></div>

CSS

body {
  background-color: #20262f;
  color: white;
}

JavaScript

//  過去使用 function constructor
let Student = function (data) {
  this.name = data.name
  this.age = data.age
  
  // prototype method
  getName(){
    // this 指稱的是所建立的 instance
    return `${this.firstName} ${this.lastName}`
  }
}

let aaron = new Student({name: 'Aaron', age: 16})
log(aaron)





function log (message) {
	let logElement = document.querySelector('#log')
  
  if (typeof message === 'object') {
  	message = JSON.stringify(message)
  } 
  
  logElement.innerHTML = message
}