-1

I was just playing with som javascript OOP, just for fun, but I get an error...

I'm trying to create classes i a class, and don't know if it's posible..

Can anyone get me on the right way ...

See my problem here: http://jsfiddle.net/wBZ4r/2/

function MyClass() {
    var server;

this.__init__ = function() {
    this.server = new this.Server();
    console.log(this.server.url);
}();

/* -------- Server Class ------------------ */
this.Server = function() {
    var url;
    this.__init__ = function() {
        this.url = "server/test.json";
    }();

    this.connect = function() {
        console.log(this.url);
    };
};
}(window.myclass = new MyClass());

Got this error: "this.Server is not a constructor"

Hope it makes sense!

7
  • 1
    which javascript libraries are you using? Commented Jun 13, 2013 at 20:28
  • None in this example,. but jQuery normally .. why? Commented Jun 13, 2013 at 20:30
  • why are you doing assignment inside a function call? Commented Jun 13, 2013 at 20:31
  • you're calling this.Server before it's defined. Commented Jun 13, 2013 at 20:33
  • You have a lot of dangling parens; I can't figure out what you're trying to do. Commented Jun 13, 2013 at 20:34

3 Answers 3

1

The main problem is that you are not returning a function from your first closure. But that aside, there are a lot of problems with what you are trying to do here. Here is an example of a more traditional style of Class. In the example I instantiate a second (Server) class with in the first.

http://jsfiddle.net/wBZ4r/5/

/**
closure to prevent others from accessing
*/
(function() {
/** 
Server class
*/
function Server() {
    this.url = "/some/url";
}

/**
Server prototype, defines connect function
*/
Server.prototype = {
    connect: function() {
        console.log(this.url);
    }
}

/**
MyClass
*/
function MyClass() {
    /**
    MyClass instansiates a new Server
    */
    this.server = new Server();
};

var thing = new MyClass();
thing.server.connect();
})();
Sign up to request clarification or add additional context in comments.

7 Comments

I'm not sure what the OP is trying to do, but I think the question is if it's possible to create nested classes.
Sure, and in this example MyClass instantiates a Server inside itself. What is the point of declaring one class within another?
Thanks for the example and I was just testing and trying thinks out... I don't know if its a stupid idea to have a class in a class! I just got an idea in my head ;)
@MorganARRAllen Its probably not a great idea, but one reason could be that it keeps the class private to the scope of the outer class.
If that is the case declare you classes in a common private scope, not within the class itself. Its just cluttering and confusing.
|
1

this.server is not defined when you call it. So it is read as undefined and fails.

changing your code to this allows it to create the object successfully:

 this.__init__ = function() {
        this.server = new this.Server();
        console.log(this.server.url);
    };

    /* -------- Server Class ------------------ */
    this.Server = function() {
        var url;
        this.__init__ = function() {
            this.url = "server/test.json";
        }();

        this.connect = function() {
            console.log(this.url);
        };
    };
    this.__init__();

You also had an issue where you assigned to this without binding it to the proper scope in your 2nd init function. That can be fixed like this:

var url,self = this;
this.__init__ = function() {
self.url = "server/test.json";
}();

Working fiddle: http://jsfiddle.net/wBZ4r/4/

2 Comments

Referencing this for scope works, but it will still leave __init__ as null. Unless that's the intention, it's probably better just to define the method, then call it.
@numbers1311407 yeah I have no idea what he was trying to do with that. Some other posters have good examples of better ways to do things that he may or may not be trying to do. I wasn't sure what he was going for so I just fixed the obvious errors to show there wasn't a fundamental problem with declaring a class inside another one
1

Your problem is that you are not using this keyword properly. I suggest you reading Javascript Garden, it's very good to learn a lot of things about JS.

function MyClass() {
    var self = this;
    /* -------- Server Class ------------------ */
    function Server() {
        var selfServer = this;
        this.__init__ = function () {
            selfServer.url = "server/test.json";
        }();

        this.connect = function () {
            console.log(selfServer.url);
        };
    };
    this.__init__ = function () {
        self.server = new Server();
        console.log(self.server.url);
    }();
    this.Server = Server;
}(window.myclass = new MyClass());

JSFiddle

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.