Pages

Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, November 4, 2008

Javascript Object Oriented Programming

Javascript is a prototype based language (not a class based language). You can read more here to understand differences between prototype based and class based language.

However, prototype has some good features that help you can implement the Object oriented programming model. Nowadays, a lot of javascript frameworks support us can implement OOP easily (like Prototype, JQuery, …). In this post, I do not want to introduce about them. I would like to help you understand about Javascript OOP without any support from other external libraries or frameworks.

Index

1. Defining a class

2. Common definitions and implementation

3. Inheritance in Javascript

1. Defining a class

To define a class in Javascript, we can use one of below ways

1. Traditional way

function className ([params] ) {

//properties and functions

}

2. Second way

className = function( [params]) {

//properties and function

}

2. Common definitions and implementation

  • private variables are declared with the ‘var’ keyword inside the object, and can only be accessed by private functions and privileged methods.Example
    function Person {
    var name = “KBird”;
    }
  • private functions are declared inline inside the object’s constructor (or alternatively may be defined via var functionName=function(){…}) and may only be called by privileged methods (including the object’s constructor).Example
    function Person {
    this.name = “KBird”;
    var getName = function()
    {
    return this.name;
    }
    };
  • privileged methods:privileged methods are declared with
    this.methodName=function
    (){…}
    and may invoked by code external to the object.
  • public properties are declared with this.variableName and may be read/written from outside the object.
  • public methods are defined by Classname.prototype.methodName = function(){…} and may be called from outside the object.
  • prototype properties are defined by Classname.prototype.propertyName = someValue
  • static properties are defined by Classname.propertyName = someValue

3. Inheritance in Javascript

Inherit from a super class

subclassName.prototype = new subClass;

Override a function

subclassName.prototype.functionNeedToOverride =function(params) {

}

To be continue

Reblog this post [with Zemanta]

Javascript history

In this post, I just collect information from Wikipedia and some portfolios of persons who makes Javascript to be success today.
Everything has its history. Understand the history of Javascript helps you have a broader knowledge and makes you more interesting in working with this great programming language.

JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, which was later renamed to LiveScript, and finally to JavaScript. The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. JavaScript was first introduced and deployed in the Netscape browser version 2.0B3 in December 1995. The naming has caused confusion, giving the impression that the language is a spin-off of Java, and it has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web-programming language.

Microsoft named its dialect of the language JScript to avoid trademark issues. JScript was first supported in Internet Explorer version 3.0, released in August 1996, and it included Y2K-compliant date functions, unlike those based on java.util.Date in JavaScript at the time. The dialects are perceived to be so similar that the terms “JavaScript” and “JScript” are often used interchangeably (including in this article). Microsoft, however, notes dozens of ways in which JScript is not ECMA compliant.

Netscape submitted JavaScript to Ecma International for standardization resulting in the standardized version named ECMAScript.

(From wikipedia: http://en.wikipedia.org/wiki/JavaScript)

Who make Javascript success?

Brendan Eich: the man invented Javascript


Brendan Eich

he created Javascript when he was in Netscape. Currently, Brendan Eich is CTO of Mozilla Corporation



...



Douglas Crockford (JSON)

Discover Javascript and JSON.

He is well known for his work in introducing JavaScript Object Notation (JSON).

You can enjoy a lot of his good articles here

http://crockford.com/

http://blog.360.yahoo.com/douglascrockford





John Resig (JQuery)

pushed Javascript forward. He is the man creates JQuery - this is one of biggest successful projects of Javascript.

Now, John Resig is a JavaScript Evangelist for the Mozilla Corporation and the author of the book Pro JavaScript Techniques.




Sam Stephenson (PROTOTYPE)

Sam Stephenson

Sam created the Prototype JavaScript framework in February of 2005 as part of the foundation for Ajax support in Ruby on Rails. He lives in Chicago, writes web applications for 37signals, and tumblelogs on Projectionist.


Javascript programming

While working with a lot of web development teams, I found a truth: most of developers (at least: Vietnamese developers) do not think Javascript is important. Most of them use javascript as tips and tricks - just google and apply every script they found to solve their problem. Parts of them try to prevent using javascript with complex technique into their project - with them Javascript is a nightmare.

I agree that writing Javascript to make the code run cross browser is complex and difficult with some developer. However, that’s not a big problem if you have a deep knowlege about Javascript. Everything has a little bit difficult starting. Right?

In this series of posts, I don’t want to dig into old knowledge about syntax, basic types, … in Javascript. If you want to know about that, you can get them with google. This series of post only focuses into these points:

1. Javascript introduction

2. Object oriented programming in Javascript

3. Unit testing with Javascript

4. Introduce some Javascript frameworks nowadays.

I am writing these parts and will post them one by one.

Wait ‘n enjoy.

Reblog this post [with Zemanta]