Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to use Javascript as OOPS for beginner

, 22 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Javascript
Hello,
This is my first tips and trick for codeproject. So I decides start from very basic tips on javascript. As most of the beginner developer use javascript something like
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>

 
This is very basic style to make fucntion and expose that function in different function like in above example function product(a,b) is directly call in body javascript.
 
This is very simple approach to use javascript. Let try to learn Javascript in more generic term. I will give demo in steps
 
Step 1: First i would recommend to make external javascript rather then in same html header although this is not mandatory. But for good coding practices Javascript should be separated in file.
Step 2: Create airthmaticOp.js
function airthmaticOperation()
{
}
Step3: as you can see in step 2, i create airthmaticOpration function which in this step i will treated function like a class.
hmmm.. So as you know in class there are methods, so how i can make methods in javascript for my class airthmaticOpration. In javascript special keyword prototype is used in order to make method for class.The prototype object is here to help when you wish to quickly add a custom property to an object that is reflected on all instances of it. To use this object, simply reference the keyword "prototype" on the object before adding the custom property to it, and this property is instantly attached to all instances of the object. A demonstration is worth a thousand words, so I'll show one right now , how to use it in my class airthmaticOperation
airthmaticOperation.prototype.addition = function(a,b)
{
  return a+b;
}
 
airthmaticOperation.prototype.multiply = function(a,b)
{
  return a*b;
}
 
 
Step 4: That looking cool Now how to use it in my function.
 
first you have to make object as you make in simple class like
 var objMyClass = new aithmaticOperation();
 
Object is created and Now you are ready to access method of my class airthmaticOperation
 
var resultAdd = objMyClass.addition(a,b);
 
var resultMul = objMyClass.multiply(a,b);
 
That's all about it. Its very good approach to make complex javascript and scalable like as OOPS.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

maq_rohit a.k.a asthanarht
Technical Lead
United States United States
Rohit is a Lead Engineer from NCR. He started Embedded Programing in his college days and now he is a Software Developer by Profession. Mainly he interested in cutting edge technology offer by Microsoft (i.e Azure,MVC). He Loves coding and his passion is always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books and hang out with friends is his most favorite past time hobby.
 

1. 20 Apr 2014: Best Mobile Article of March 2014 - First Prize
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 5 Very good article to understand the ... PinmemberAnjum.Rizwi10-Dec-11 19:39 
GeneralThanks Abdur Pinmembermaq_rohit7-Feb-11 22:10 
Thanks Abdur
GeneralBrilliant help for a beginer. PinmemberAbdur Rashid7-Feb-11 18:02 
GeneralReason for my vote of 5 great post and article for beginner.... Pinmemberhugevishal22-Nov-10 6:18 
GeneralReason for my vote of 4 I like this article, easy to underst... Pinmember300521804622-Nov-10 3:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 22 Nov 2010
Article Copyright 2010 by maq_rohit a.k.a asthanarht
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid