javascript scope issue

WBOY
Release: 2023-03-01 21:58:01
Original
815 people have browsed it

PHP code:

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

Output

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

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>
Copy after login
Copy after login

Output:

<code>[ 1, 2, 9 ]
[ 1, 2, 9 ]
[ 1, 2, 9 ]       
</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Output

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

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>
Copy after login
Copy after login

Output:

<code>[ 1, 2, 9 ]
[ 1, 2, 9 ]
[ 1, 2, 9 ]       
</code>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!