Here, we are designing a function named change() that has an argument and we are trying to change the value of the passed argument inside the function, but it will not effect to the main/actual argument that is passed as the argument while calling.
在這里,我們正在設計一個名為change()的函數,該函數具有一個參數,并且試圖更改該函數內部傳遞的參數的值,但不會影響在調用時作為參數傳遞的main / actual參數。
Example:
例:
a =10
Value before function call: a = 10
//calling function
change(a)
//changing inside the function
a = 67
Value inside the function: a = 67
//printing the value after the function call
Value after the function call: a =10
Code:
碼:
<html lang="en">
<head>
<script>
function change(a){
a=67;
document.write("Inside Function: A = "+a+"<Br />");
}
</script>
</head>
<body>
<script>
var a=10;
document.write("Before Calling : A = "+a+"<br />");
change(a);
document.write("After Calling : A = "+a+"<br />");
</script>
</body>
</html>
Output
輸出量
Before Calling : A = 10
Inside Function: A = 67
After Calling : A = 10
翻譯自: https://www.includehelp.com/code-snippets/call-by-value-in-function-javascript.aspx