I am trying to create a function to automatically create object and then alert its properties. However, there seems to be some problem as I cannot get the alerts on click of button. Can someone help?
<html>
<head>
<script>
var player = function (x, y, z, i) {
return :{
firstName: x,
lastName: y,
quote: z,
salary: function (i) {
return (i + 3000)
}
};
}
var Saurav = player('Saurav', 'Ganguli', 'Bengali Babu', 1000);
function alertify() {
alert(Saurav.firstName);
alert(Saurav.lastName);
alert(Saurav.quote);
alert(Saurav.salary(2000));
}
</script>
</head>
<body>
<button onclick="alertify()">Click Me!</button>
</body>
</html>
Rajan Goswami
7691 gold badge5 silver badges17 bronze badges
asked Oct 28, 2015 at 12:47
Deadpool
8,2969 gold badges49 silver badges96 bronze badges
1 Answer 1
<button onclick="alertify()">Click Me!</button>
<script>
var player = function(x, y, z, i) {
return { // <---- remove ":"
firstName: x,
lastName: y,
quote: z,
salary: function(i) {
return (i + 3000)
}
};
}
var Saurav = player('Saurav', 'Ganguli', 'Bengali Babu', 1000);
function alertify() {
alert(Saurav.firstName);
alert(Saurav.lastName);
alert(Saurav.quote);
alert(Saurav.salary(2000));
}
</script>
answered Oct 28, 2015 at 12:51
ozil
7,1739 gold badges38 silver badges62 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
default
return:{firstName, the:should not be there afterreturn