pay attention to the environment. This is a piece of Node.js back-end code, not the js code in html.
the assignment function has the same parameter as the variable name. How to assign the value without changing the parameter name?
// test.js
var x;// x
init(12)// x
console.log(`x:${x}`);//
// ,?
function init(x){
// nodejs,window.x=x
x = x;
}
there is another question: is the variable x in this code a global variable or a local variable?
if it is the js code in html, x is undoubtedly a global variable belonging to the window object.
but if it is in the back-end environment of Node.js, the global object is the global, user code, not in the outermost layer, only those defined in this.x or var global.x are global variables.
my guess is:
the js file is a module in Node.js, then x should be a local variable belonging to the module, and the local variable defined in the internal function belongs to the subordinate relationship of the scope chain.
global object scope-> module object scope-> function object scope
but why do you use this in the module to point to global?