Home > Computers & Technology > Software > Software & Web Development
Created on: May 06, 2009
Introduction
The aim of this article is to quickly make you understand the meaning of Programming Objects (OOP) and how to create them in JavaScript. I assume here that you know the meaning of variables and functions in JavaScript, but you might not have understood JavaScript objects. There are three ways of creating JavaScript objects and in this article I will show you the one that is indicated by the title (of this article). Let us start.
Variables, Functions and Objects in JavaScript
Let us look at two variables and a function. The two variables hold two numbers. The function adds the two numbers. These are the two variables and the functions:
<script type="text/javascript">
var num1 = 5;
var num2 = 8;
function add ()
{
answer = num1 + num2;
return answer;
}
var result = add();
alert(result);
</script>
As you can see in the code, there are two variables. The function adds the values of the two variables, and returns the answer. Down in the code, the function is called, and the return value is assigned to a new variable, result. The last statement alerts (displays) the result.
Now, in programming (in a program code) you would find some functions that work with certain variables, such that if you remove one of the variables in the code, the rest of the variables and the functions will not be very useful; also, if you remove one of the functions, the rest of the functions and the variables will not be very useful. In other words, the variables and functions in question form a set. The above group of statements is such a set. Would it not be nice if we had such a set in one unit? In fact, many modern programs, have such a set in one unit called an object. Many modern computer languages have each, its own syntax for creating an object. JavaScript has three ways of doing this and in this article we learn a way I describe as Creating JavaScript Objects by Inheritance.
Properties and Methods
A programming object fundamentally has what is called properties and methods. This is not a big deal. In the above script we have a function, which works with two variables. If we are to make this set, an object, then the variables would become the properties and the function would become the method. You can have more than one method in an object, just as you can have more than one property. As you can see it is not a big
Below are the top articles rated and ranked by Helium members on:
Creating JavaScript objects using Inheritance
Helium Debate
Cast your vote!
Which is a better Web application framework: Ruby on Rails or PHP?
Click for your side.
Featured Partner
My hope is that every person with cancer can smile because someone touched his or her life. So many of you made Nicki smile! I never imagined that I would devote my life to this cause, but when cancer touched my life it changed everyth...more