Home  >  Article  >  Backend Development  >  javascript scope issue

javascript scope issue

WBOY
WBOYOriginal
2016-12-01 00:56:32816browse

PHP code:

<code><?php
$key = [1,2];
function add($key){
    array_push($key,'dddss');
    print_r($key);
}
add($key);
print_r($key);</code>

Output

<code>Array
(
    [0] => 1
    [1] => 2
    [2] => dddss
)
Array
(
    [0] => 1
    [1] => 2
)
</code>

JS code:

<code>var $key = [1,2];
function addkey($key){
    let hello = $key;
    hello.push(9)
    console.log(hello)
    console.log($key)
}
addkey($key)
console.log($key)</code>

Output:

<code>[ 1, 2, 9 ]
[ 1, 2, 9 ]
[ 1, 2, 9 ]       
</code>

What I want is that the processing inside the function does not affect the results outside the function, which is similar to the results of PHP. I also want to know the reason

Reply content:

PHP code:

<code><?php
$key = [1,2];
function add($key){
    array_push($key,'dddss');
    print_r($key);
}
add($key);
print_r($key);</code>

Output

<code>Array
(
    [0] => 1
    [1] => 2
    [2] => dddss
)
Array
(
    [0] => 1
    [1] => 2
)
</code>

JS code:

<code>var $key = [1,2];
function addkey($key){
    let hello = $key;
    hello.push(9)
    console.log(hello)
    console.log($key)
}
addkey($key)
console.log($key)</code>

Output:

<code>[ 1, 2, 9 ]
[ 1, 2, 9 ]
[ 1, 2, 9 ]       
</code>

What I want is that the processing inside the function does not affect the results outside the function, which is similar to the results of PHP. I also want to know the reason

let hello = $key.slice();

Your function parameter is an array, and arrays are passed by reference in js. . When you use let inside a function to assign a reference to an array to another variable. In fact, both hello and $key point to a value at the same time, which is an array of length 2 in this case. So when you push or do other operations on any variable, what changes is actually the value pointed to by both variables at the same time.

PHP’s default incoming parameters are copies, not references, but JS always passes references, so you only need to copy one in it to change it

<code class="javascript">var $keys = [1, 2];
function addkey($keys) {
    // 拷贝一份
    let hello = [].concat($keys);
    hello.push(9);
    console.log(hello);
    console.log($keys);
}
addkey($keys);
console.log($keys);</code>

The problem is passing by reference. Actually this he will appear in PHP.

<code>$obj = new stdClass();
$obj->name = 'aa';
function change($o) {
    $o->name = 'bb';
}
change($obj);
echo $obj; // 'bb'</code>

The difference is that js arrays are also objects, and they are also passed by reference. You can search for 'clone object'. The answer to this question has been given above.

When using php to pass in parameters, adding & means passing by reference, and it will be the same as when your js code comes out

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn