简体   繁体   中英

Javascript global variable doesn't work while local does

I'm trying to make a sound when a button is pressed so the following code is inside onclick="cNote()" . Now, when I declare the variable locally it's working:

function cNote() {
var cNote = new Audio("cNote.wav");
cNote.play();

But when I'm declaring it as a global variable it doesn't work:

var cNote = new Audio("cNote.wav");

function cNote() {
cNote,play();
}

Any idea what's the issue?

You've given the variable and the function the same name. There can only be one symbol with a given name in any scope.

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