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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Javeed M ShaikhRank #839 · 2.7K reputationPosted Oct 20, 2011, 5:55 PM EDT
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".
Satyapriya NayakRank #47 · 31.5K reputationPosted Oct 20, 2011, 8:04 PM EDT
Please refer the below link
http://www.codeproject.com/KB/scripting/CustomObjectJavaScript.aspx
Thanks
PRAVEEN SAINI5 reputationPosted Oct 20, 2011, 11:26 PM EDT
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...
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 21, 2011, 1:26 AM EDT
Use the following Link,
Click Here for Reference to Javascript Functions and Classes