4/24 6th社課
還沒有個人網站 退幹做吧
const myPizza = {
crust: "thin",
toppings: ["cheese"],
isHot: true,
price: 15,
eat(){
console.log("You ate the pizza")
},
upgrade(){
this.price += 10
this.toppings = ["cheese", "ham", "mushroom"]
}
};
console.log(myPizza.price, myPizza.toppings);
myPizza.upgrade();
console.log(myPizza.price, myPizza.toppings);output
偷一個
function Liuqi(name) {
this.name = name;
setInterval(function sixSeven(){
console.log(`${this.name} did a six seven🫴🫳.`)
}, 1000);
}
var p = new Liuqi("Jason");這邊的sixSeven()會認為裡面的this是全域的,而非liuqi()裡的,所以會找不到name這個屬性。
function Liuqi(name) {
this.name = name;
setInterval(() => {
console.log(`${this.name} did a six seven🫴🫳.`)
}, 1000);
}
var p = new Liuqi("Jason");箭頭函式會找上下文來看
繼承:
function Mac(){
this.name = "Big Mac";
this.ingredient = ["Beef","Sauce","Lettuce","Cheese","Onions","Pickles","Bread"];
this.kcal = 550;
}
let myOrder = new Mac();
Mac.prototype.comment = "No Beef";
console.log(myOrder.name);
console.log(myOrder.kcal);
console.log(myOrder.comment);
console.log(myOrder.forHereOrToGo);建構式:之後會被new呼叫的函式
幫「Mac的prototype」建立新屬性
沒有指定,會回去找
new:建立新的物件(產出實體)
constructor
output
| name | Big Mac |
| ingredient | ["Beef","Sauce", ...] |
| kcal | 550 |
| 上一層 | Mac.prototype |
myOrder
Mac.prototype
| 建構式 | Mac |
| 上一層 | Object.prototype |
Mac
| 原型 | Mac.prototype |
| 上一層 | Function.prototype |
Object.prototype
| (一堆內建方法) |
| 上一層:NULL |
NULL
*Mac不會出現在myOrder的原型鏈上
...
| name | Big Mac |
| ingredient | ["Beef","Sauce", ...] |
| kcal | 550 |
| 上一層 | Mac.prototype |
myOrder
Mac.prototype
| 建構式 | Mac |
| comment | No Beef |
| 上一層 | Object.prototype |
Mac
| 原型 | Mac.prototype |
| 上一層 | Function.prototype |
Object.prototype
| (一堆內建方法) |
| 上一層:NULL |
NULL
...
| name | Big Mac |
| ingredient | ["Beef","Sauce", ...] |
| kcal | 550 |
| 上一層 | Mac.prototype |
myOrder
Mac.prototype
| 建構式 | Mac |
| comment | No Beef |
| 上一層 | Object.prototype |
Mac
| 原型 | Mac.prototype |
| 上一層 | Function.prototype |
Object.prototype
| (一堆內建方法) |
| 上一層:NULL |
NULL
console.log(myOrder.comment)
console.log(myOrder.name)
console.log(myOrder.ingredient)
console.log(myOrder.forHereOrToGo)
...
name = "Jason";
console.log(`Hello ${name}!`);name = "Jason";
console.log('Hello ' + name + '!');簡單的語法糖例子:模板字串
class Goblin{
constructor(name, yearsBeingSingle, job){
this.name = name;
this.yearsBeingSingle = yearsBeingSingle;
this.job = job;
}
chant(){
return "哥布林!在一起!強大!";
}
backToCave(){
return `${this.name},該跟族長回山洞了`;
}
}
const g = new Goblin("熾熱哥布林", 18, "學生");
console.log(g.yearsBeingSingle);
console.log(g.backToCave());
console.log(g.chant());建構式:定義屬性&有繼承的話會呼叫父類別(一個class只能有一個)
一樣要用new實體化
output
class Goblin{
#name;
#yearsBeingSingle = 0;
#job;
constructor(name, yearsBeingSingle, job){
this.#name = name;
this.#yearsBeingSingle = yearsBeingSingle;
this.#job = job;
}
chant(){
return "哥布林!在一起!強大!";
}
backToCave(){
return `${this.#name},該跟族長回山洞了`;
}
}
const g = new Goblin("熾熱哥布林", 18, "學生");
console.log(g.backToCave());
console.log(g.chant());加「#」前綴創建(before:加「 _ 」)
外部不可引用
e.g. console.log(g.#name)
class Goblin{
//...
get info(){ //getter
return `我的名字叫${this.#name}, 單身${this.#yearsBeingSingle}年, 是個${this.#job}`;
}
set setYearsBeingSingle(years){ //setter
this.#yearsBeingSingle = years;
}
//...
}
const g = new Goblin("熾熱哥布林", 18, "學生");
console.log(g.info);
g.setYearsBeingSingle = 19;
console.log(g.info);output
要用"="
class Goblin{
//...
static #numOfGoblins = 0;
constructor(name, yearsBeingSingle, job){
//...
Goblin.#countGoblins();
}
static #countGoblins(){
this.#numOfGoblins++;
}
static get numOfGoblins(){
return this.#numOfGoblins;
}
}
const g1 = new Goblin("熾熱哥布林","18","學生");
console.log(Goblin.numOfGoblins);
const g2 = new Goblin("冰霜哥布林","24","超商店員");
console.log(Goblin.numOfGoblins);記得加static
#countGoblins()是個靜態又隱私的方法,在每次實體化的時候會呼叫
用numOfGoblins()這個靜態的getter從外部取得數量
output
class Goblin{
constructor(name, yearsBeingSingle, job){
this.name = name;
this.yearsBeingSingle = yearsBeingSingle;
this.job = job;
}
chant(){
return "哥布林!在一起!強大!";
}
}
class GoblinLeader extends Goblin{
constructor(name, yearsBeingSingle, job, order){
super(name, yearsBeingSingle, job);
this.order = order;
}
chant(){
return `${super.chant()} 哥布林們聽我號令:${this.order}`;
}
}
const l = new GoblinLeader("長老", 100, "長老", "回山洞!");
console.log(l.chant());Js裡會用import/require 跟 export 來告訴電腦你要引什麼
function add(a, b){
return a + b;
}
function multiply(a, b){
return a * b;
}
module.exports = {add, multiply};math.js
const math = require("./math.js");
console.log(math.add(2, 3));
console.log(math.multiply(4, 6));主要js檔案
輸出
輸入檔案路徑,引入整個檔案
export function add(a, b){
return a + b;
}
export function multiply(a, b){
return a * b;
}math.js
import {add, multiply} from './math.js';
console.log(add(2, 3));
console.log(multiply(4, 6)); 主要js檔案
直接用關鍵字 export 匯出函式
從math.js引入add、multiply兩個函式
import * as myModule from "/modules/my-module.js";補充一個