1215 JS

for(var i = 0; i < 5; i++) {
}

console.log(i)

==>印出5
在這裏for不是function,外層也取的到var = i,同時i會試到5才停止,所以最後console.log會印出5
如果是用let宣告則會被for迴圈的block綁住,則會出錯
=====================

for(var i = 0; i < 3; i++) {
 setTimeout(function(){
 console.log(i)
 },
 i*1000)
}

console.log(i)

印出3,3,3,等非同步執行console.log時,i已經是3
改用let宣告才能印出0,1,2(面試愛考)
======================
英雄製造器1130-1247
const guko = heroCreate('悟空','氣功')

function heroCreate(name,action){
const h = {}
name,
action,
console.log($this.)
}

解構寫法:
let name = goku.name
let action = goku.action
==>
let {name, action} = goku

const h = Object.create(actions)
==>以actions為參考原型(proto)建立出h,h可以擁有actions的內容
JS中所有的物件都有__proto__方法找他的proto(原型)
aaaa.proto.__proto__這種用法不好效能也差
1155
Object.getPrototypeOF(a)

============================
function hero(){
console.log("hi")
}
const h = hero()
console.log(h)
==>印出hi是const h = hero()印出
undefind是console.log(h)印出,因為在js中function沒用return就沒有回傳值1403之後

所有function都有prototype的屬性,aaa.prototype預設為一個{}
所有物件都有__proto__

================
const h2 = new hero()

  1. 進入泡泡
  2. 建立空物件{}
  3. this -> {}
  4. 把{}.proto -> function.prototype
  5. return this,用new時會蓋掉function裡的return值回傳this

proto 和.prototype不一樣
1523
============================

this

https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

規則 1. 函數執行的時候,有沒有使用 new 關鍵字?如果有,this 就是指向那個物件本身。
規則 2. 「誰呼叫,誰就是 this」規則。
規則 3. 是否使用箭頭函數?有的話就不會有自己的 this
規則 4. 是否有使用 bindapply 或是 call 方法?有的話 this 的指向也會跟著改變。
規則 5. 是否有開啟「嚴格模式」?

attack.apply(h) attack.bind(h) attack.call(h)三者差異(面試題)

測試題

var hero = {
  name: '悟空',
  sayMyName: function() {
    console.log(this.name);
  }
};

hero.sayMyName();   // A

var speakOut = hero.sayMyName;
speakOut();         // B

const someone = { name: '路人' }

hero.sayMyName.call(someone);  // C

function here() {
  console.log(this);
}

const there = () => {
  console.log(this);
}

here();   // D
there();  // E