In a place where your fantasy can roam free, there won't be any boundaries to what your imagination can create.
Misha’s Tech Playground
graphics, fun and code play
I'm crazy - but who cares? My ideas are constantly dwelling and this is the place for them to pour out and form into something good. Only active participation will ultimately make this place come to life.
Your Own JavaScript Data Structures
Some Data Goodies
To keep one or the other occupied I thought why not give away some insights on how to implement advanced data structures in JavaScript. Read on to learn how to use Stacks, Vectors and various types of linked lists in your JavaScript Application - complete with Source code.
The Problem
Beside primitive types, Javascript has only built in Objects and Arrays (which themselves are Objects internally as well). While this is a convenience drawback, this also results in a great flexibility. This article will show you how to implement your own data structures utilizing JavaScript object’s prototype property to create your own data types. I’ve added all objects to my global Object CGE, you might need to adjust that to fit your code.
Although not necessary for these Objects to work, I’ll also provide a version of each Object that was made utilizing the AJS.Class constructor, thus only working when the AJS Library is present.
(more…)
Tags: JavaScript, oop
Thoughts about OOP advantage
Having recently refactored my JavaScript Game Engine (or what it will eventually become one) and the associated Level Editor to use OOP techniques I noticed that this has some issues besides the obvious advantages.
Using OOP can save disk space
In one particular case I read about memory requirements of large tile maps on Xalthorn's Blog. A commenter (horRAiN) mentioned that his map used up about 5 MiB if he saved it to a text file using the php serialize command on the server.
Due to it's nature the serialize command has a serious overhead which can in some cases be between 30% – 50%, even more so if you just serialize an array containing objects and / or a huge number of duplicate objects.
The serialize command is useful, no question, but for things like saving a game level or map it is just a huge waste of space, even though it is the easiest and fastest solution (in the short term).
In the long run it is better to make use of Objects and Object references. Saving an array of 200 x 200 Map Elements containing just references and a description of each object obviously saves loads of space and troubles in the long run.
A randomly created map with 40.000 reference numbers and the 18 used objects, formatted as xml takes up 128 KiB.
Using OOP frees up your namespace
Creating an application requires a substantial amount of variables to hold all the data you'll need at runtime. Using objects will most likely save you from overwriting one or the other variable you need at a certain point.
Especially in Javascript wrapping your variables and functions into an object means that you'll save the Interpreter a lot of work at runtime.
You have to access any variable or function directly.
Java(Script) example:
-
myObject.myOtherObject.myValue = value;
You might not see the immediate benefit in this, but here are some (some can be translated to php as well):
- Your variables are isolated and can't conflict with reserved variables or functions.
- Your code is more reusable because you can use the same vars and functions on multiple objects (myEventObject.add, myDomElement.add, myOtheObject.add)
without having to to think about naming conflicts - The interpreter is able to look up your variables faster because the global object doesn't grow as much (each variable and function call starts looking in the global object and digs down)
OOP needs more discipline
What I noticed coding on the game engine and the editor was that using objects literally enforces a disciplined usage of coding conventions.
Because you can use object variables, object functions, local functions and local variables it is important to keep an overview of what you have defined. Variables should be declared at first in a function or object to make it more clear what variables are used inside this statement.
Objects can be costly
If you use Classes to define new Objects, any object you create needs memory. Think first if you really need an object to perform a certain task or if it is enough to call the functions from a static context.
In Java and PHP you would call functions and variables by prepending the class name (without the need to instantiating an object first:
However, at this point there is a little trick that JavaScript provides to make this somewhat easier. JavaScript has something called JSON (JavaScript Object Notation), which basically lets you define an object structure that won't be able to have instances. This has the advantage that you don't need the longish prototype notation to declare the objects. But to explain this to you I'll hand over to the excellent tutorial by Dustin Diaz – JSON for the masses. Tags: oop
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.