JavaScript Object-Oriented Programming Part 1

This article was written in 2001 and still remains one of our most popular posts. If you’re keen to learn more about JavaScript, you may find this recent article on TypeScript of great interest.

It may be shocking news, but JavaScript is a very powerful object-based (or prototype-based, whatever you wish to call it) language. Yes, JavaScript is a powerful language, not just something that’s handy for image rollovers and other corny, flashy effects. However, very few people who have used JavaScript realize its capabilities. If you’re one of these people, this tutorial is aimed at you.

First of all, JavaScript is not a full-blown OOP (Object-Oriented Programming) language, such as Java or PHP, but it is an object-based language. So, why should you use objects? Not only do they help you better understand how JavaScript works, but in large scripts, you can create self-contained JavaScript objects, rather than the procedural code you may be using now. This also allows you to reuse code more often.

I hope that this article will turn an intermediate JavaScripter who’s itching to learn objects, into an expert, keen on the exciting object-oriented JavaScript world!

In this tutorial, you’ll learn:

  • JavaScript’s primitive data types
  • What an object is in JavaScript
  • How to create custom objects
  • What a constructor is
  • What an object’s prototype property is

JavaScript’s Primitive Data Types

JavaScript has five primitive data types:

  • Undefined,
  • Null,
  • Boolean,
  • Number, and
  • String.
    Throughout this tutorial, we’ll use the latter three extensively.

A Boolean is a logical entity that consists of either a true or a false value. An example of one is:

var BooleanValue = true;

A Number is a set of numerical digits that represent a number. Through this tutorial, we’ll only use base-10 numbers. An example:

var NumericalValue = 354;

A String is a set of zero or more characters. An example:

var StringValue = "This is a String";

Typeof

A less-known operator in JavaScript is the typeof operator. It tells you what type of data you’re dealing with. Makes sense, huh? Let’s look at some examples:

var BooleanValue = true; 
var NumericalValue = 354;
var StringValue = "This is a String";
alert(typeof BooleanValue) // displays "boolean"
alert(typeof NumericalValue) // displays "number"
alert(typeof StringValue) // displays "string"
Go to page: 1 | 2 | 3 | 4

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • http://pavang.com Pavan Gudhe

    its simply awesome……