<!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>
js中参数都是按值传递的
原创徐同保的博客 ©著作权
文章标签 js 文章分类 JavaScript 前端开发
上一篇:js地图-leaflet
下一篇:js单例模式
-
js 按值传递
红宝石中的定义:ECMAScript中所有函数的参数都是按值传递的三种类型:按值传递,
按值传递 传递对象 引用传递 -
JS是按值传递还是按引用传递?
JS是按值传递还是按引用传递?
JS是按值传递还是按引用传递? -
js和java的参数传递方式实际都是一样的,都是按值传递
只不过要注意的是字符串类型在java中是对象,在js是基本数据类型,作为参数传递的时候是拷贝值,
字符串 java 参数传递 基本数据类型 其他 -
java的按值传递参数 java传递参数的方式
java中的方法可以传递参数,参数传递的方法就是值传递。参数又分为实参和形参。
java System 参数传递 值传递