admin管理员组

文章数量:1415467

What am doing wrong. I try to make object but when i try to initialize i get this error in console: I try to put all in document.ready and whitout that but dont work. In both case i have some error. Am new sorry for dumb question

ReferenceError: Circle is not defined
    var obj = new Circle;

JS

$(function(){
        var Circle = {

                init: function() {
                    console.log("Circle initialized");
                }     
        };

});

HTML

<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src=".8.3/jquery.min.js"></script>
        <script src="javascript/circle.js"></script>

        <script>           

               $(document).ready(function(){
                   var obj = new Circle;

                   obj.init();
               })

        </script>

    </head>
    <body>
        <div id="test" >TODO write content</div>
    </body>
</html>

NEW UPDATE

$(function(){

        window.Circle = {

                init: function() {
                    console.log("Circle initialized");
                }     
        };
       window.Circle.init();
});

....

  <head>

     <script>           

        window.Circle().init();

    </script>

</head>

What am doing wrong. I try to make object but when i try to initialize i get this error in console: I try to put all in document.ready and whitout that but dont work. In both case i have some error. Am new sorry for dumb question

ReferenceError: Circle is not defined
    var obj = new Circle;

JS

$(function(){
        var Circle = {

                init: function() {
                    console.log("Circle initialized");
                }     
        };

});

HTML

<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="https://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="javascript/circle.js"></script>

        <script>           

               $(document).ready(function(){
                   var obj = new Circle;

                   obj.init();
               })

        </script>

    </head>
    <body>
        <div id="test" >TODO write content</div>
    </body>
</html>

NEW UPDATE

$(function(){

        window.Circle = {

                init: function() {
                    console.log("Circle initialized");
                }     
        };
       window.Circle.init();
});

....

  <head>

     <script>           

        window.Circle().init();

    </script>

</head>
Share Improve this question edited Aug 10, 2013 at 14:36 Jony asked Aug 10, 2013 at 14:13 JonyJony 5992 gold badges6 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You've defined your "Circle" function inside another function — the anonymous function you pass in as a a "ready" handler. Therefore, that symbol ("Circle") is private to that function, and not visible to the other code.

You can make it global like this:

window.Circle = {
  // ...
};

You could also add it to the jQuery namespace (may or may not be appropriate; depends on what you're doing), or you could develop your own namespace for your application code. Or, finally, you could consider bining your jQuery "ready" code so that the "Circle" object and the code that uses it all appears in the same handler.

editanother possibility is to move your "Circle" declaration pletely out of the "ready" handler. If all you do is initialize that object, and your property values don't require any work that requires the DOM or other not-yet-available resources, you can just get rid of the $(function() { ... }) wrapper.

1) you are assigning Circle in a function context, not as a global. You can only use it there unless you expose it to global. 2) you are calling Circle as a constructor, but Circle is not a function.

This solves both issues:

var Circle = function () {};
Circle.prototype.init = function () {
    console.log('Circle initialized.');
};
var obj = new Circle();
obj.init();

本文标签: jqueryWhy do I get this JavaScript ReferenceErrorStack Overflow