js中参数都是按值传递的



<!DOCTYPE html>            
<html lang="en">            
<head>            
<meta charset="UTF-8">     
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">     
<title>js中参数都是按值传递的</title>          
<style type="text/css">      
</style>           
</head>            
<body>     
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>      
<script type="text/javascript">      
//js中参数都是按值传递的,fun1中重新定义了一个数组,也就是现在堆内存中有两个数组,
//外部的arr指向的是老的数组,被传入参数指向的是新定义的数组,
//fun2中arr和myarr指向了同一个对象,而在myarr中修改了数组,其实修改了它们共同指向的数组
var arr = [1, 2];
function fun1 (myarr) {
    myarr = [];
}
function fun2 (myarr) {
    myarr.push(3);
}
fun1(arr); console.log(arr);  //1,2
fun2(arr); console.log(arr);  //1,2,3
</script>      
</body>            
</html>  




原文地址:https://www.cnblogs.com/xutongbao/p/9924849.html