Pass by value & Pass by reference
https://www.youtube.com/watch?v=-hBJz2PPIVE&ab_channel=WebDevSimplified
如何傳~
傳值與傳參考是傳遞參數給函數的兩種方法。在傳值中,引數的值被複製並傳遞到函數。在傳參考中,引數的記憶體位置的參考被傳遞到函數。
在 TypeScript 中,所有的基本類型例如數字、字串和布林值都是傳值。這意味著值的副本被傳遞給函數,而在函數內部對引數所做的任何更改都不會在函數外部反映出來。
function increment(num: number) {
num = num + 1;
console.log(num); // Output: 6
}
let num1 = 5;
increment(num1);
console.log(num1); // Output: 5
在上面的示例中,increment 函數以數字為引數並將其增加 1。然而,當我們使用變量 num1 調用 increment 函數時,num1 的值不會改變,因為它是傳值。
另一方面,非基本類型例如陣列和物件是傳參考的。這意味著引數的記憶體位置的參考被傳遞到函數,而在函數內部對引數所做的任何更改都會在函數外部反映出來。
function addItem(arr: string[], item: string) {
arr.push(item);
console.log(arr); // Output: ["apple", "banana", "cherry"]
}
let fruits = ["apple", "banana"];
addItem(fruits, "cherry");
console.log(fruits); // Output: ["apple", "banana", "cherry"]
在上面的示例中,addItem 函數以陣列和一個項目作為引數,並將項目添加到陣列中。當我們使用 fruits 陣列調用 addItem 函數時,fruits 的值被更改,因為它是傳參考的。
總之,在 TypeScript 編程中了解傳值和傳參考之間的差異非常重要,因為它可以影響你的代碼行為。
比較
三個等於的情況下,如果要比較的是物件,則比較的是物件的記憶體位置,而不是物件的值,如果比較的是值,則比較的是值的大小,當然他們的類型也要一樣
使用三個等於(===)比較兩個變數值時,在比較之前會先判斷它們的型別是否相同。如果型別不同,那麼直接返回false。如果型別相同,則比較它們的值(value)是否相等。如果是比較物件或陣列,則比較的是它們所在記憶體位置是否相同。
在 TypeScript 中,== 和 === 都是比較運算符,用於比較兩個值是否相等。其中,== 用於比較兩個值是否相等,而 === 用於比較兩個值是否相等且類型相同。
在使用 == 比較兩個值時,如果它們的類型不同,TypeScript 會嘗試進行類型轉換,然後再進行比較。例如,5 == "5" 會返回 true,因為 TypeScript 將字串 "5" 轉換為數字 5。
在使用 === 比較兩個值時,如果它們的類型不同,則不會進行類型轉換,並且如果類型不同,則返回 false。例如,5 === "5" 會返回 false,因為 5 是一個數字類型,而 "5" 是一個字串類型。
在 TypeScript 中,=== 更常用,因為它可以避免類型轉換帶來的問題。
如果要比較兩個變量的引用是否相同,可以使用 Object.is() 方法。例如,Object.is({}, {}) 會返回 false,因為它們是不同的物件引用。
對於物件的比較,== 和 === 會比較它們的引用,而不是它們的屬性。如果比較的兩個物件的引用相同,則它們相等,否則它們不相等。因此,即使兩個物件具有相同的屬性,但如果它們的引用不同,它們仍然不相等。
const obj1 = { name: "Alice" };
const obj2 = { name: "Alice" };
console.log(obj1 == obj2); // Output: false
console.log(obj1 === obj2); // Output: false
console.log(Object.is(obj1, obj2)); // Output: false
在上面的例子中,obj1 和 obj2 具有相同的屬性,但是它們的引用不同,因此它們使用 ==,=== 和 Object.is() 都會返回 false。
如果要比較兩個物件的屬性是否相同,則需要手動進行比較。一種方法是使用 JSON.stringify() 方法將物件轉換為 JSON 字符串,然後比較字符串是否相同。
const obj1 = { name: "Alice" };
const obj2 = { name: "Alice" };
console.log(JSON.stringify(obj1) === JSON.stringify(obj2)); // Output: true
在上面的例子中,JSON.stringify(obj1) 和 JSON.stringify(obj2) 返回相同的字符串,因此它們使用 === 比較會返回 true。
Pass by value & Pass by reference
Pass by value and pass by reference are two methods of passing arguments to a function. In pass by value, the value of the argument is copied and passed to the function. In pass by reference, a reference to the memory location of the argument is passed to the function.
In TypeScript, all primitive types such as number, string, and boolean are passed by value. This means that a copy of the value is passed to the function, and any changes made to the argument inside the function are not reflected outside the function.
function increment(num: number) {
num = num + 1;
console.log(num); // Output: 6
}
let num1 = 5;
increment(num1);
console.log(num1); // Output: 5
In the above example, the increment function takes a number as an argument and increments it by 1. However, when we call the increment function with the num1 variable, the value of num1 is not changed because it is passed by value.
On the other hand, non-primitive types such as arrays and objects are passed by reference. This means that a reference to the memory location of the argument is passed to the function, and any changes made to the argument inside the function are reflected outside the function.
function addItem(arr: string[], item: string) {
arr.push(item);
console.log(arr); // Output: ["apple", "banana", "cherry"]
}
let fruits = ["apple", "banana"];
addItem(fruits, "cherry");
console.log(fruits); // Output: ["apple", "banana", "cherry"]
In the above example, the addItem function takes an array and an item as arguments and adds the item to the array. When we call the addItem function with the fruits array, the value of fruits is changed because it is passed by reference.
In conclusion, understanding the difference between pass by value and pass by reference is crucial in TypeScript programming as it can affect how your code behaves.