先看題1chrome
var obj = {name:"a"} function bar(obj) { console.log(obj.name); obj = {name:"b"}; console.log(obj.name) } console.log(bar(obj)) console.log(obj.name)
在chrome控制檯執行如下看看,發現依次打印函數
a
b
a
函數體內從新賦值了,新開闢了內存區域,跟外面的引用類型無關了。
題2,稍微有點變化spa
var obj = {name:"a"} function bar(obj) { console.log(obj.name); obj.name = "b"; console.log(obj.name) } console.log(bar(obj)) console.log(obj.name)
在chrome控制檯執行如下看看,發現依次打印code
a
b
b
引用類型值發生改變了
題3,再稍微變化一下blog
var obj = {name:"a"} function bar(obj) { console.log(obj.name); var obj = {name:"b"}; console.log(obj.name) } console.log(bar(obj)) console.log(obj.name)
在chrome控制檯執行如下看看,發現依次打印內存
a
b
a
題4,繼續變化io
var obj = "name" function bar(obj) { console.log(obj); var obj = "age"; console.log(obj) } console.log(bar(obj)) console.log(obj)
發現以此打印console
name
age
name
題5,繼續變化function
var obj = "name" function bar() { console.log(obj); var obj = "age"; console.log(obj) } console.log(bar(obj)) console.log(obj)
發現依次打印class
undefined
age
name
題6,仍是繼續變化
var obj = "name" function bar() { console.log(obj); obj = "age"; console.log(obj) } console.log(bar(obj)) console.log(obj)
瞧瞧此次又會打印啥
name
age
age
若是你都答對了,說明 你理解了引用傳遞和值傳遞 ,理解了局部變量和全局變量,若是你答錯了,好好想一想估計也就想通了