Pass by value and pass by reference in javaScript (If the passed parameter is an object, is it passed by value or by reference?)

 If the javascript parameter is an object, is it passed by value or by reference?

Value Passing and Reference Passing in Java and Javascript

Detailed explanation of the principle of JavaScript function parameter transfer-value transfer or reference transfer

javaScript the pass by value and pass by reference (if parameters are passed object , it is passed by value or pass by reference ?)

Data types in javaScript can be divided into two categories:

Primitive data type value primitive type, such as Undefined, Null, Boolean, Number, String.

Reference type value , that is, Object type, such as Object, Array, Function, Date, etc.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javaScript中按值传递与按引用传递</title>
<script type="text/javascript">
//javascript传递参数如果是object的话,是按值传递还是按引用传递?
function setName(obj){
	obj.name = '令狐冲';
}
var person = new Object();
setName(person);
console.log(person.name);//令狐冲

//再看下面的代码,有意思的来了(参考这篇文章https://www.zhihu.com/question/27114726)
function setName2(obj){
	obj.name = '杨幂';
	obj = new Object();
	obj.name = '刘亦菲';
}
var person2 = new Object();
setName2(person2);
console.log(person2.name); //杨幂

console.log('***********************');

//整数类型
var a = 666;
console.log('a=' + a); //a=666
function fn1(parameter){
	parameter = 888;
}
fn1(a);
console.log('a=' + a);//a=666
console.log('***********************');

//浮点类型
var b = 77.86;
console.log('b=' + b);//b=77.86
function fn2(parameter){
	parameter = 55.42;
}
fn2(b);
console.log('b=' + b);//b=77.86
console.log('***********************');

//boolean类型
var c = true;
console.log('c=' + c);//c=true
function fn3(parameter){
	parameter = false;
}
fn3(c);
console.log('c=' + c);//c=true
console.log('***********************');

//数组类型
var d = ['韦小宝', '阿珂', '建宁公主', '双儿', '苏荃', '曾柔', '方怡', '沐剑屏'];
console.log('d=' + d);//d=韦小宝,阿珂,建宁公主,双儿,苏荃,曾柔,方怡,沐剑屏
function fn4(parameter){
	parameter.splice(1, 0, '康熙', '鳌拜', '吴三桂');
}
fn4(d);
console.log('d=' + d);//d=韦小宝,康熙,鳌拜,吴三桂,阿珂,建宁公主,双儿,苏荃,曾柔,方怡,沐剑屏
console.log('***********************');

//Date类型
var myDate = new Date();
console.log('myDate=' + myDate);//myDate=Tue Mar 09 2021 16:22:54 GMT+0800 (中国标准时间)
function fn5(parameter){
	parameter.setFullYear(2088, 9, 15);
}
fn5(myDate);
console.log('myDate=' + myDate);//myDate=Fri Oct 15 2088 16:22:54 GMT+0800 (中国标准时间)
console.log('***********************');
</script>
</head>
<body>
<h2>javaScript中值传递与引用传递</h2>
</body>
</html>

The results of the operation are as follows:

If the string type is missing, add

//字符串类型
var homeTown = '江西省赣州市于都县';
console.log('homeTown=' + homeTown);//homeTown=江西省赣州市于都县
function fn6(parameter){
	parameter = '于都县';
}
fn6(homeTown);
console.log('homeTown=' + homeTown);//homeTown=江西省赣州市于都县
console.log('***********************');

The results of the operation are as follows:

Add some other cases

var testC = {a : 666};
console.log(testC);//Object { a: 666 }
function testObject(example){
    example = {b : 57};
}
testObject(testC);
console.log(testC);//Object { a: 666 }

console.log('***************');

var testB = {h : 37};
console.log(testB);//Object { h: 37 }
function testObj(example) {
    example.aa = 89;
}
testObj(testB);
console.log(testB);//Object { h: 37, aa: 89 }

console.log('***************');

var names = ['韦小宝', '阿珂', '双儿', '建宁公主', '曾柔', '方怡', '苏荃', '沐剑屏'];
//Array(8) [ "韦小宝", "阿珂", "双儿", "建宁公主", "曾柔", "方怡", "苏荃", "沐剑屏" ]
console.log(names);
function fn1(parm){
	var obj1 = parm;
	obj1.splice(1, 0, '康熙', '鳌拜');
}
fn1(names);
//Array(10) [ "韦小宝", "康熙", "鳌拜", "阿珂", "双儿", "建宁公主", "曾柔", "方怡", "苏荃", "沐剑屏" ]
console.log(names);

console.log('***************');

var citys = ['于都县', '兴国县', '赣县'];
console.log(citys);//Array(3) [ "于都县", "兴国县", "赣县" ]
function fn2(parm){
	var obj1 = parm;
	obj1 = new Array("南昌市", "景德镇市", "赣州市");
// 	obj1 = null;
}
fn2(citys);
console.log(citys);//Array(3) [ "于都县", "兴国县", "赣县" ]

console.log('***************');

The results of the operation are as follows:

Other cases

var citys = ['于都县', '兴国县', '赣县'];
var t = citys.slice();
console.log(t);//Array(3) [ "于都县", "兴国县", "赣县" ]
var t2 = citys.slice(1);
console.log(t2);//Array [ "兴国县", "赣县" ]
var t3 = citys.slice(1, 2);
console.log(t3);//Array [ "兴国县" ]

The results of the operation are as follows:

Value Passing and Reference Passing in Java and Javascript

Pass by value and pass by reference, other cases

//值传递
var a = 2;
var b = a; // b是a的值的一个副本
console.log(a);// 2
console.log(b);// 2
b++;
console.log(a);// 2
console.log(b);// 3

The results of the operation are as follows:

//引用传递
var c = [1,2,3];
var d = c; // d是[1,2,3]的一个引用,同指一块内存区域
console.log(c);//Array(3) [ 1, 2, 3 ]
console.log(d);//Array(3) [ 1, 2, 3 ]
d.push(4);

console.log(d); // Array(4) [ 1, 2, 3, 4 ]
console.log(c); // Array(4) [ 1, 2, 3, 4 ] 修改同指的那片内存区域,因此c也跟着变化了

d = [4,5,6];
console.log(d);//Array(3) [ 4, 5, 6 ]
//d指向一片新的内存区域[4,5,6],不会改变c的指向,因此c还是[1,2,3,4]
console.log(c);//Array(4) [ 1, 2, 3, 4 ]

The results of the operation are as follows:

//引用副本传递
	function foo(x) {
		x.push(4);
		console.log(x, "******");
		//然后
		x.length = 0; // 清空数组
		x.push(4, 5, 6, 7);
		console.log(x, "######");
	}

	var a = [ 1, 2, 3 ];
	foo(a);
	console.log(a); // 是[4,5,6,7],不是[1,2,3,4]
	a = [ 1, 2, 3, 4 ];
	foo(a.slice());//a.slice()传入的是a的浅复制副本,因此不会改变a的那块内存区域
	console.log(a);//还是[1,2,3,4]
	foo(a);
	console.log(a);//[4,5,6,7]

The results of the operation are as follows:

Guess you like

Origin blog.csdn.net/czh500/article/details/114587637