Jordan Baron Jordan Baron - 2 months ago 16
Javascript Question

My function is undefined

I am trying to fix an error in my JavaScript. I defined a function, but when I try to call that function, it gives me an error saying it is undefined.



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>

<script>
var Library = {
name: "Timmy",
greet: function(){
alert("Hello from the " + Library.name + " library.");
}
}
library.greet();
</script>


</body>
</html>




Answer

you defined your object as Library, which is uppercase

in order to make this work you should use

var Library = {
    name: "Timmy",
    greet: function(){
        alert("Hello from the " + Library.name + " library.");
    }
}
Library.greet();
Comments