The name is
Mike Myat Min Han aka 'mmhan'.

I am a developer, mostly do development on Javascript, PHP and CakePHP alongside some development work on C, C++, Obj-C, C# and AS. I also have a thing for Python.

I post here all sorts of crap that I find worthy of sharing: Quotes, Jokes and Notes.

You can check out my blog and follow me on twitter.


Text

Apr 27, 2011
@ 5:04 pm
Permalink

How to define a Class in Javascript

First of all, There is no such thing as a Class in Javascript. It’s all objects. Hence use functions and literals to simulate “Class”y development.

 
//Way 1: Function simulating class. 
function Phone(model){    
    this.model = model; 
} 
myPhone = new Phone("Android");  

//Way 2: literal as singleton 
var Phone = {
    model: 'Android' 
};  

//Way 3: Singleton into function 
var Phone  = new function(){     
    this.model = "Android" 
}

Read Stoyan Stefnov’s article for more.