OOP JavaScript overview - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Webmaster Support (https://www.supportforums.net/forumdisplay.php?fid=36) +---- Forum: Website Development (https://www.supportforums.net/forumdisplay.php?fid=43) +---- Thread: OOP JavaScript overview (/showthread.php?tid=3001) |
OOP JavaScript overview - Gaijin - 11-18-2009 Let me show you how to work with Objects in JavaScript. This is just a simple overview of an creation and use of a JavaScript Object. First we need to create our class, we do that with the keyword function. Code: function jsClass() { Yes this does looks just like a simple javascript function for now, but we will turn that into an Object. To acces your Class all you need to do is to create a new Instance of the object, new instance is created with the keyword new, followed by the name of the Object. In this case that would be jsClass. Code: var myClass = new jsClass(); There you go, the variable myClass is now a instance of our jsClass object and can access all it's methods and properties. But our class doesn't have methods and properties, so let's create some. Properties and methods are created with the keyword this following by a DOT (.) and a name of the method/property. Code: function jsClass() { Now we have a Class filled with few members, but let us make it more functional and create a simple Calculator class. Code: var myClass; // this is our global variable for the class instance Save the above code as "tut.js". That is so simple that I don't need to explain it with big words, if you read it few times you'll notice by your self what what is. Methods are functions inside your class, preperties are variables. Code: this.property; To use this class now, we create a new simple HTML file. Code: <html> Now we create a DIV element for our results. This is where we will show the result of the calculation. Code: <div id="result"></div> So far so good, let us now initialize our Object/Class. Code: <script language="javascript" type="text/javascript"> And now follows our form. Code: <form name="calc"> And that's actually it, you should now understand how JavaScript and OOP works, this is not near everything about OOP. So I can only say read everything you can get. If you have any question post them here and I will try to answer them! Source JavaScript (tut.js) (Click to View) Source HTML (index.htm) (Click to View) RE: OOP JavaScript overview - Nyx- - 12-06-2009 awesome tutorial ^_^ thanks for this RE: OOP JavaScript overview - Gaijin - 12-07-2009 Thank you! It's nothing big, but JS oop is simple. RE: OOP JavaScript overview - Sagittarius - 12-11-2009 thank poster.... thank... Good Tutorial |