aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Initializing variables on object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Initializing variables on object creation" Watch "Initializing variables on object creation" New topic
Author

Initializing variables on object creation

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Hello. I've made a few small and simple Javascript applications and so far I've just used global variables. Now I'm tring to get my head around the way Javascript handles objects. I'm trying to encapsulate my code. One thing I'm missing is the way I would use constructors in Java to initialize the variables It needed. How do you go about doing this in Javascript?

This is what I have so far. I have made a namespace called Memory game, and so far I have 2 objects. One for the whole game, and one for the screen which will contain all the drawing methods. I've given both objects init methods and I call them right after they have been created. In these methods I put the code t I would usually put in a constructor in Java. The memory game needs a Screen object so I create it in it's init method. From that init method I call the Screen's init method which gets the canvas and context of the document.

So is this good, bad, standard pratice or just plain wrong? I did try giving each object an auto executing function, but they couldn't find the variables when I used this. They thought 'this' referred to the window.

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
As often happens after posting a topic I see a better way of doing things , which I'll put below. However there are times when you need an init / Java style constructor, and I'm still interested in knowing how things are done.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Any function can be a constructor -- it's invoking the function with the new operator that makes two special things happen:
  • A new blank object is created
  • The new object is implicitly "passed" to the construtor as the this parameter. (In other words, it becomes the function context.)

  • The prototype property of the constructor can be used to assign members to every object created through the constructor.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Neil Cartmell
    Ranch Hand

    Joined: Feb 13, 2010
    Posts: 150
    Bear Bibeault wrote:Any function can be a constructor -- it's invoking the function with the new operator that makes two special things happen:
  • A new blank object is created
  • The new object is implicitly "passed" to the construtor as the this parameter. (In other words, it becomes the function context.)

  • The prototype property of the constructor can be used to assign members to every object created through the constructor.


    Hi I appreciate the reply, but I don't think you really answered my question. I know what a constructor is. And I know any function can be a contructor. My example even used a method init() to demonstrate this. My question was more about good coding practice in JavaScript.

    Never mind though. I think I now know what I need to do.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61434
        
      67

    Part of good coding practice means following conventions. Naming a constructor init() isn't. A constructor should be a noun sand start with a capital letter. It describes what is being created.
    Neil Cartmell
    Ranch Hand

    Joined: Feb 13, 2010
    Posts: 150
    Bear Bibeault wrote:Part of good coding practice means following conventions. Naming a constructor init() isn't. A constructor should be a noun sand start with a capital letter. It describes what is being created.


    I was thinking of these Constructor functions (or whatever they are called) as being exactly the same as a Java class. So I was thinking you have your variables then your methods and that's it. But now I realize that as it is actually a function itself, any code inside it that is not in another function will run in order. So I can use it just like a Java constructor. I think...
    Eric Pascarello
    author
    Rancher

    Joined: Nov 08, 2001
    Posts: 15376
        
        6
    Pick up JavaScript The Good Parts by Crockford. I think it will help you.

    Eric
    Neil Cartmell
    Ranch Hand

    Joined: Feb 13, 2010
    Posts: 150
    Eric Pascarello wrote:Pick up JavaScript The Good Parts by Crockford. I think it will help you.

    Eric


    I ordered it the other day. It should be here any day now. I've heard a lot of good things about it.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Initializing variables on object creation