Object-Oriented JavaScript


I've known for a long time that JavaScript is object oriented, but apparently, I've been using an antiquated syntax. New JavaScript frameworks like Prototype and Dojo, use property lists to define new objects. Here's a simple example I pulled from the Dojo documentation:

var exampleObj = {
counter: 0,
foo: function(){
alert("foo");
this.counter++;
},
bar: function(){
alert("bar");
this.counter++;
}
};

Here's some examples of how you might use this new object:

exampleObj.foo();
exampleObj.bar();
alert( exampleObj.counter );

Man, I've gotten behind the times. I thought I had mastered most everything JavaScript had to offer years ago. The world was moving on without me. Forgive me while I attempt to catch up.

0 comments:

Post a Comment

Aaron Hursman
Aaron Hursman is a passionate user-advocate who is lucky enough to do what he loves for a living. As a user experience architect, he applies user-centered design principles and techniques including user research, persona development, information architecture, storyboards, wireframes, prototyping, visual design, graphic design, interaction design, and usability. Aaron has a background in web development, enterprise applications, and the social web. At nGame, he is applying his craft to design and build the next generation of enterprise software. Aaron is available as a speaker and author upon request.
Disclaimer: The information in this website is provided "as is" with no warranties, and confers no rights. This website does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my own personal opinion. Inappropriate comments will be deleted at the authors discretion. All instructions and code samples (if any, ever) are provided "as is" without warranty of any kind, either express or implied.