Differences between php and node (1) -- function scope

In PHP, the scope of variables within a function is limited to curly braces.
In node, the variable scope inside the function can take the value of the variable outside the function.
The common denominator, of course, is that neither outside can take the value inside.

php code:
<?php
$s = 123;

function test ()
{
    echo $s;
}
test();

The print result of the above code is:
Notice: Undefined variable: s in D:\workspace_utf8\guai2\public\public\test\1.php on line 6

php reported an error.

node code:
var s = 123;
function test()
{
    console.log(s);
}
test();


The above print result is 123.

In addition, node can not only read, but also write variables
var s = 123;
(function test()
{
    s = 12345;
})();
console.log(s);


The print result of the above code is 12345, the value of the variable was changed by the anonymous function.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567464&siteId=291194637