This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Feb 13, 2010
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 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.
Never mind though. I think I now know what I need to do.
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.
Joined: Feb 13, 2010
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...