JS struct and id issue
Hello, all,
I am trying to increase my understanding of JavaScript structs (commonly referred to as 'associative array').
I did a Google search and found an example creating an object called "Gimli". Now structs can contain strings, integers, or even functions. But if you reference a function, you need to append () to the function name.
Just playing around, trying to learn, I've got the following, but the last button is not working, even though I use () in the id attribute.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Gimli test</title>
<script src="jquery-1.11.3.js"></script>
<script>
var Gimli = {
name: "Gimli",
race: "Dwarf",
weapon: "axe",
age: "139",
greet: function(){return 'Hi, my name is $(this.name)!';}
}
</script>
</head>
<body>
<div id='displayThis'> </div>
<div id='chooseThis'>
<button id="name">Name</button>
<button id="race">Race</button>
<button id="weapon">Weapon</button>
<button id="age">Age</button>
<button id="greet()">Greet</button>
</div>
<script>
$('button').on('click',function(){
switch(Gimli.hasOwnProperty(this.id)){
case true:
alert(Gimli[""+this.id]);
break;
default:
alert('Aint happening');
break;
}
});
</script>
</body>
</html>
Any insight appreciated.
V/r,
^ _ ^
