by reference (傳參考)、by value(傳值),undefined 與 null 的差異

Javascrip的call by value

var james = 1;
var bill;
bill = james;
james = 2;
console.log(bill);// call by value , 雖然James改成2, 但bill還是1 , var james = { age : 1 }; // 因為James是物件型別!所以call by reference
var bill;
bill = james;
james.age = 2;
console.log(bill.age);// call by reference, bill.age = 2

Call by value的型別
Boolean:布林
Null:空值
Undefined:未定義
Number:數字
String:字串
Symbol(於 ECMAScript 6 新定義):符號

Call by reference的型別
Object
Array
Function

Undefined和Null的差異

Undefine:
1.變數沒給初始值,就是undefined
2.若原本function需要傳入參數,但你沒傳入,則回傳值為undefined
3.使用void運動符,後面的變數不管是什麼,都回傳undefined


1.var num;
console.log(num);
2.function myfun(x){ console.log(x);}
myfun();
3.console.log(void 123);
console.log(void 'string');
console.log(void null);
console.log(void undefined);

null:
1.null是代表空值,沒東西
2.若DOM回傳元素不存在,回傳null

1.var value = null;
console.log(“value = “ + value);
2.console.log( document.querySelect('h1'));

NaN: Not a Number
發生在把非數字傳到數字型別時。

null:這地方會有一個值,但這個值目前還沒準備好的意思,所以先填入 null
undefined:這地方沒有這個東西,所以你無法使用

NaN:要轉型成數字時傳入參數非數字的時候

Ref
1.https://medium.com/itsems-frontend/javascript-pass-by-value-reference-sharing-5d6095ae030b

2.
https://snh90100.medium.com/javascript%E4%B8%ADundefined%E5%92%8Cnull%E7%9A%84%E5%B7%AE%E5%88%A5-1f48e9be5e02

3.
https://sweeteason.pixnet.net/blog/post/43007183-javascript-%E5%9F%BA%E7%A4%8E%E6%89%93%E5%BA%95%E7%B3%BB%E5%88%97-(%E4%BA%8C)---%E9%97%9C%E6%96%BC-null%E3%80%81undefine

--

--

Jameskrauser Lee

For the last few years. i was involved mostly in the development of Automatic Fare collection system for the Chennai Metro. Familiar with C++ and iOS.