HI,
how to create a class within a JavaScript?
thanks..
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Oct 21, 2011, 5:26 AM
Use the following Link,
Click Here for Reference to Javascript Functions and Classes
PRAVEEN SAINIPosted Oct 21, 2011, 3:26 AM
Although we read in books that we can not have user define objects in the javascript but we can have user define classes and objects in javascript. Yet not have any class keyword in javascript !
now I am giving an example of class in javascript ---
var MyClass = function(){
// this is the class area or we can say constructor area
// we can call any method from here (that method is to be called when an instance has been created).
// like any initialization work or binding event at run time.
this.initialize();
}
// now we are adding the methods and properties to the class.
// we add the properties and methods in boject literal notation. Ex. : ( propertyName:'propertyvalue' , methodName: function(){ })
MyClass.prototype = {
myFirstProp: 'propertyValue',
mySecondValue: 'propValue',
initialize: function(){
// work to be done at run time.
// element to be created at run time.
// binding events to the elements
this.bindEvents();
alert('class initialization completed');
},
bindEvents: function(){
// bind the events to the elements.
}
}
// now we can create object of this class as we create in languages.
var myObject = new MyClass();
alert(myObject.myFirstProp); // giving the value - propertyValue (value of the first property of the class)
Thanks...
If you like this, Please mark as accepted answer...
Satyapriya NayakPosted Oct 21, 2011, 12:04 AM
Please refer the below link
http://www.codeproject.com/KB/scripting/CustomObjectJavaScript.aspx
Thanks
Javeed M ShaikhPosted Oct 20, 2011, 9:55 PM
There is not concept of classes in Javascript but you can simulate that by writing functions like this :
function Car (type) {
this.color = "red";
}
var mycar = new Car('ferrari');
mycar.color = "reddish";
Please do not forget to mark "Accepted Answer".