简体   繁体   中英

How to make variable from function in Javascript become global?

i use onclick="namefunction('name')" on my html

and make a function to catch that:

function show(name){
   bobi = name;
}

Question.. how to use boby in global? outside that function

Using window.bobi isnt working, globalThis.bobi isnt working too, declare var bobi; on above that function isnt working too.

Sorry i am noob

Declaring a variable with var makes it global because var is global scope. So if you write like below it should be your answer:

var bobi;
function show(name){
   bobi = name;
}

show('hello world')

console.log(bobi) // it returns 'hello world'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM