Better understanding of JavaScript options

I do not understand some parts of JavaScript code regarding parameters. I found this example in W3schools:

<!DOCTYPE html>
<html>
<body>

<a id="myAnchor" href="http://w3schools.com/">Go to W3Schools.com</a>

<p>The preventDefault() method will prevent the link above from following the URL.</p>

<script>
document.getElementById("myAnchor").addEventListener("click", function(event){
    event.preventDefault()
});
</script>

</body>
</html>

I am confused with the internal parameter function event. This code works, although the parameter eventdid not become an argument, that is, it does not matter. How can I use this "empty" parameter using the method. Why does this code work? I'm new to JavaScript, so any simple answer would be appreciated.

+4
source share
2 answers

crevice,

, "click", " ". "click" , " ", " ()", 1 , "". , , . , , , "event.preventDefault(); alert (" test ")", , , "test" .

, . .

0

. - .

, :

window.addEventListener("click", click);

function click() {
    console.log(arguments);
}

, [MouseEvent]. arguments , . click MouseEvent, window addEventListener .

:

init("abc", logArgs); //--> "abc"
init("abc", logDog); //--> "dog"

function logArgs(x) {
    console.log(x);
}

function logDog(x) {
    console.log("dog");
}

function init(x, callback) {
    callback(x);
}

, . - . , :

init("abc", function(y) { //the y parameter comes from the init function
    console.log(y); //"abc"
});

function init(x, callback) {
    callback(x); //send the first argument to the anonymous function
}
0

All Articles