一些關於 JS 的神秘事件,往往是我們能持續學習,
並繼續鑽研下去的動力。以下四個例子
var Jim = {
favMovie: "LaLaLand",
favBook: "The Hitchhiker's Guide to the Galaxy"
}
var Albert = Jim
Albert.favMovie = "Fight Club"
console.log(Jim.favMovie)
//結果:"Fight Club"
const arr = [1, 2, 3]
arr.push(4)
//結果:(4) [1, 2, 3, 4]
function hello () {
console.log(arguments)
}
undefined
hello('echoooo')
結果:Arguments ["echoooo", callee: ƒ, Symbol(Symbol.iterator): ƒ]
var obj = {
sayHi: "How are you?",
sayHello: function(){
console.log(this.sayHi)
}
}
function sayHello(obj) {
console.log(this)
obj.sayHello()
}
sayHello(obj);
結果:
Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
VM1856:4 How are you?
想知道答案通常就必須更了解語言背後的特性,也往往難以一言以敝之,需要搭配理論、實作與推導的過程。
從好奇開始,之後我們了解更多便能更接近這些神秘問題的答案!