Objective
This problem actually puzzled me for a long time, but in the actual use always muddle through, do not want to go deep. Because of this attitude, in the process of learning Javascript, the level has been out of half a bucket of water state, many concepts and principles indefinitely, vague.
So, wrote a series of "JavaScript ..." is to correct the attitude, seriously study the characteristics and principles of JavaScript, to lay a solid foundation.
Today, this article explores the question of function-passing: is the function argument a reference to a value or a pass?
1. If a reference is passed
var name = ' JS '; function ChangeName (name) { = ' Javascript ';
console.log (' name changed: ' +name) // ' JS '
Why doesn't the name change?
If the reference is passed, the name should be changed. So it should be a value pass?
It may be normal for you to subconsciously feel the same, but it's because your experience tells you. Let's take a look at the next possibility by pressing the table.
2. If the value is passed
var obj = {name: ' JS '}; function changeobjname (obj) { = ' Javascript '; Console.log (' obj.name changed: ' +obj.name); // obj.name Changed:javascript // {name: ' Javascript '}
Why is the parameter passed this time changed? Is it not a value pass? So that's a quote?
What about the above example? Start confused ...
3. Is the value pass or the reference pass??
In fact, in the world of Javascript, function transfer is not necessarily a way of transmitting parameters, mainly depends on the parameter passed. There are two main types of situations:
1. If the value type
Variables of basic data types are basically value types, such as String, numeric, Boolean, and so on.
The argument to a value type is a value pass, and modifications to this type parameter within a function do not affect the value that was originally passed in.
2. If the reference type
Composite data types such as objects, arrays, and so on are reference types.
Reference passes are reference passing, and modifications to this type parameter within a function affect the value that was originally passed in.
4. Mechanisms for in-depth function transfer
Whether the function parameter is a value pass or a reference pass in Javascript