Click here to Skip to main content
Click here to Skip to main content
Go to top

Creating Custom Class in JavaScript and adding methods, properties.

, 29 Aug 2006
Rate this:
Please Sign up or sign in to vote.
This is a brief example of creating custom object in JavaScript.

Introduction

This is a brief example of creating custom object in JavaScript.

Creating Class

For creating class in javascript first we have to create the function whose name should be same as class. This function is called as constructer.

For example I want to create the class Named MyClass so function name should be MyClass

function MyClass()
{ 
    //This function is same as a constructer 
    alert("New Object Created"); 
}

We can also add the argumnets in this function.

Now I am making object of MyClass

//Creating Object 
var MyObject = new MyClass (); 

Adding new method and property in "MyClass"

NewObject.prototype = 
{ 
    //Adding Method named "MyMethod" 
    MyMethod: function(){alert("My Method");} , 
 
    //Adding property named "MyProperty" 
    MyProperty: "My Property" 
}

Calling method and assigning value to property

//Calling Method 
MyObject.MyMethod(); 

//Assigning Property 
MyObject.MyProperty = "My Property Value changed"; 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

MaheshSingh
Web Developer
India India
Around 4 years of programming, application and product development experience in Microsoft Technology (ASP.NET 1.x, ASP.NET 2.0, C#, VB.NET, SQL Server and Oracle). Currently working in Adobe Syatem India, Noida

Comments and Discussions

 
GeneralMy vote of 3 Pinmembergtankaria18-Jan-13 1:42 

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
Web04 | 2.8.140916.1 | Last Updated 29 Aug 2006
Article Copyright 2006 by MaheshSingh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid