JavaScript is passed by value, but it is the case to know that the original value will change after delivery, or there will be a bug that you can't think of.
One, by value pass--meta type input
Tip: The arguments passed in by the meta-type (number, string, Boolean, null, undefined) are not affected by the invocation of the incoming variable, regardless of how it is manipulated inside the function.
For example:
var arr = [1, 2, 3, 4]var str = ' 1234 'function= input + ' 5678 'console.log (' time Calls (Zhaò) ', input)return input; Console.log (' seventh set of Calisthenics ', Guangboticao7 (str), str);
Output values for the Times in Call (Zhaò) 12345678
Second, pass by value--reference type input
There are two different situations.
1. When the function internally modifies its property (attribute), the incoming variable is affected and changed!!! If the eighth set of calisthenics in the code below, push (add array at the end, return index number) to the ARR array, arr will be changed.
2. When ARR is re-assigned within the function, i.e. newvalue!==oldvalue (completely unequal), incoming external variables will not be affected, i.e. they will not be changed!!! As the nineth set of calisthenics of the code below, arr is [1, 2, 3, 4, [5, 6, 7, 8]], the function is internally re-assigned, arr will not be changed, still output [1, 2, 3, 4, [5, 6, 7, 8]]. and concat The array, returns a new array.
Example code:
var arr = [1, 2, 3, 4] function Guangboticao8 (input) { return input.push ([5, 6, 7, 8])} function Guangboticao9 (input) {input = ' 2234 ' return [Input].concat ([5, 6, 7, 8 ' Eighth set calisthenics ' // Span style= "COLOR: #008000" > output "Eighth set calisthenics" 5 [1, 2, 3, 4, [5, 6, 7, 8]] console.log ( ' nineth set calisthenics ' // Span style= "COLOR: #008000" > "Nineth set calisthenics" ["2234", 5, 6, 7, 8] [1, 2, 3, 4, [5, 6, 7, 8]]
Summary: JS is passed by value, but in case of confusion, it is usually divided into three cases
1. When the metadata type is a pass-through value, when the intrinsic function makes any modifications to the passed value, it does not change its external pass value.
2. When the reference data type is passed the value, when the intrinsic function modifies the passed value, i.e. push (), Unshift (), concat (), pop (), Shift (), splice (), slice (), reverse (), sort () will make the transfer value change.
3. When the reference data type is passed a value, the passed value is not changed when the intrinsic function re-assigns the value to the passed value, even if it is newvalue!==oldvalue (completely unequal).
Exercises:
var Lilei = {name: ' Li Lei '}
var Hanmeimei = {name: ' Han Meimei '}
var jim = {name: ' Jim '}
var students = [Lilei, Hanmeimei]
function change (s) {
S[0] = Jim;
S[1].name = ' Lucy '
}
Change (students);
Console.log (students)
Console.log (Lilei)
Console.log (Hanmeimei)
Console.log (Jim)
The result is?!!!!
Job Results
Reference: https://zhuanlan.zhihu.com/p/24080761
is JavaScript passed by value or by reference?