user1050619
user1050619

Reputation: 20856

javascript error while loading the document

Im trying to run the below script to understand the Javascript object and inheritance but don't see anything being displayed.

<html>
    <head>
        <script>
            $(document).ready(

                function Person(){
                    alert('New Person Created');
                }


                Person.prototype.sayHello = new function(){
                    alert('Hello');
                };

                var x = new Person();
                x.sayHello();

                var newfunction = x.sayHello;
                newfunction.call(Person);

            );
        </script>
    </head>
    <body>
    </body>
</html>

Upvotes: 0

Views: 32

Answers (4)

RoneRackal
RoneRackal

Reputation: 1233

The only thing I can see wrong is that you are trying to use the jQuery library, but you've never actually included it.

Upvotes: 0

Nagendra Rao
Nagendra Rao

Reputation: 7152

$ is defined in jQuery, you need to include jQuery library before using the $

you can include jquery library using cdn like this,

<script src ="//code.jquery.com/jquery-1.10.2.min.js"></script>

Upvotes: 1

Steve H.
Steve H.

Reputation: 6947

When you use a construct like $(document), you are calling a function $, which is defined as jQuery. You need a <script> tag in your document to load the correct version of jQuery. Also, check your browser console. You will see an error there about $

Upvotes: 0

Rob Baillie
Rob Baillie

Reputation: 3460

The first line of your script is jQuery. If you want to use jQuery you should include it first (based on what you have written I strongly suspect you don't need or want it just yet).

Alternatively, just drop the $(document).ready part and its {}s and that should get you going.

Also, take a look at your developer tools menu and get your JavaScript console open. It will have told you about this error.

Upvotes: 0

Related Questions