0

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>
1
  • 1
    You have a typo in return:{firstName, the : should not be there after return Commented Oct 28, 2015 at 12:52

1 Answer 1

2
<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.