1 Objects Wed Mar 09, 2011 7:09 pm
Unknown Data
Registered Member
Variables Learn JavaScript - Part 4 |
I mentioned earlier that JS is an objectoriented programminglanguage. And the concept "object" will I try to define in this lesson. Objects is a variable type, that can handle multiple values and functions at the same time. If we take a look at the syntax, can it look like this.
The easiest way to explain objects, is by calling them a thing - like a car. But a car isn't just a car, there are many types of cars, versions etc. It's actually the same in JavaScript. JS sees objects as elements in a browser. That could fx. be elements in a form, or a new browser window. Properties The properties is what objects got. As an example has a car wheels, and that's the property in this context. And again is it the same, when we're talking about JS. A browser window got a title, and therefore is the title the property. Another property could be the status bar. Note: The cars wheels is also an object in itself, and not just a property. So an object can also contains objects. Methods Methods is what our object can do, and in our example can the car drive. In JS can we as an example click on a button; click() or open a window open(). So methods is explained by what someone can do or what there can happen, fx. dance, write, walk, burn. In other words, can we best compare methods with verbs. If we as an example take the phrase from our Hello World script, will the brackets indicate that there is a reference to the method, and not the property. Merging it These 3 elements can we now put together and form a bether description of our object. The individual elements is separated by a dot. So it will look like this now.
As mentioned earlier can an object contain an object. Are we using the object document, then can it contain the property location, that's inself is also an object. But the location object contains the property href, which creates: document.location.href Note: It's always a good idea to give an object an uniquely name, don't use names like window1, window2 etc. In the next lesson will we learn about the functions. |
Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators. |