Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing variables on object creation

 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Neil Cartmell
    Ranch Hand
    Posts: 150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65118
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 15385
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pick up JavaScript The Good Parts by Crockford. I think it will help you.

    Eric
     
    Neil Cartmell
    Ranch Hand
    Posts: 150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic