'object-oriented' category

Ask Dan : Procedural VS object-oriented in javascript

Since the beginning of Ask Dan a javascript question, I received a bunch of questions. Here’s the first one I received. It’s from Andrew Worcester.
There seems (to me anyway) to be an overuse of the “new” syntax in libraries. If I create an Ajax object is there a functional benefit to using: “new Ajax.Request(options)” verses […]

Read the complete article...

How to inherit classes in javascript

First of all, since I made the front page of Ajaxian, a lot of new readers joined in. Welcome to all of you. I would also take a moment to explain that Javascript Kata is a technical blog about javascript and though I will talk here and there about ajax, it is not focused on […]

Read the complete article...

How to use the self with object-oriented javascript and closures

When I began with object-oriented javascript, I always saw a self here and there without fully understanding what it meant.
Self?
When you see self in some object-oriented javascript, it’s just mean that the developer is using a closure that will reference the current object via a variable named self. Because self is a variable, it could […]

Read the complete article...

How to make a singleton in javascript

Singleton is one of the most common and easiest design pattern. In fact, a lot of designs are wrong just because the developer didn’t know about singletons and design patterns (I could have said : a lot of my designs were wrong just because I didn’t know about singletons and design patterns).
I was looking at […]

Read the complete article...

Looping through object properties and hash tables using the “in” statement

As I said earlier, objects are hash tables of properties and functions so looping throught them is done the same way (weepee!).
The “in” statement
The in statement must be used in a for loop. Like this : for (var element in allElements). In most of the other languages, the element variable would contain the complete element […]

Read the complete article...

How to write constants in javascript

YOU CAN’T. As simple as that. Anyway, what is a constant? A constant is a unchangeable variable that throws an error when you try to write in it. Thus, implementing constant in javascript would be against its will to be opened. The magic of javascript resides in the liberty it gives you to change anything […]

Read the complete article...

How to do class functions in javascript (aka static or shared functions)

One of the really concept of the object-oriented programming is the class functions but they are also known as static functions (Java, php, C++) or shared functions (.NET).
Why using class method?
Class methods help you keeping your code clean by centralizing your functions. Example, you create a calendar object that formats a string from two dates […]

Read the complete article...

How to create objects in object-oriented javascript

Javascript is a functional programming language thus having no “real” objects. You can write it the way you want : procedural spag, functional, object or whatever word you know. Why should I write it object-oriented? Because, OO has proven many times its ease of use and great encapsulation. In this paper, I won’t talk about […]

Read the complete article...

How to use anonymous objects

I don’t know how to call it and I don’t even know if it has a name. I thought a moment about calling it Mistigri but I already have a cat with that name. I decided to call it anonymous object.
How?
[source:javascript]
var ao = {
name : “Anonymous Object”,
possibleName : “Mistigri”,
presentYourself : function() {
alert(”Hi! My name is […]

Read the complete article...

5 reasons to write object-oriented (oo) javascript

To write hard as a rock javascript, the first thing you need to do is to object-orient your code. I do not mean to have a class structure that could stand the passage of a sandstorm in the Sahara but a light and useful structure that will let you work elegantly.
This article does not explain […]

Read the complete article...