Objects, Properties and Methods in JavaScript
JavaScript is an Object Oriented Language. It means that it treats DOM elements as objects with properties, methods and own classes.
Objects
First, let's think about the objects. An object is a thing of any kind. A cat, a computer and a bicycle are objects in the physical world.
The objects that JavaScript treats in Web browsers are windows, forms and control elements of forms, such as buttons and checkboxes.
As there is more than one cat, or more than one window, it is advisable to assign a name to them.
Although you could refer to your pets as cat 1 and cat 2, it is not a good idea, for two reasons: first, because it is easier to call the cats if they have unique names, and second, because it is simply discourteous.
Tip
Note that the scripts you can see on the Internet will refer to objects such as window [0] and from [1]. It's not right, for the reasons already discussed, and you'll discover that it's easier for you to remember the objects in your scripts if you assign them names instead of numbers.
Properties
Objects have properties. A cat has hair, the computer has a keyboard and the bicycle has wheels.
In the JavaScript world, a window has a title, and a form can have a check box.
The properties modify the objects, and the same property name can be applied to completely different objects.
Suppose you have a property called empty. You can use vacuum wherever you want, both to say that the stomach of the cat is empty, and to say that your dining room is empty.
Note that the computer keyboard and bicycle wheels are not just properties; but also objects, which in turn can have properties. So objects can have subobjects.
Methods
Things that objects can do are called methods. Cats purr, computers break, and bicycles roll.
JavaScript objects also have methods: click () for buttons, open () for windows, and text can be selected using the selected () method. The parentheses indicate that we are referring to a method, and not to a property.
Tip
may be useful to think of objects and properties as names, and methods as verbs. The first are things, and the second, actions that those things can do, or that can be done to them.
How to put the pieces together
You can put the objects, properties and methods together to get a better description of an object, or to describe a process.
In JavaScript, all these pieces are separated by points. This is called point syntax.
Here are some examples of objects and their properties written this way:
- bycicle.wheel
- cat.claws.front.left
- computer.disc.cdrom
- document.image.name
- window.state
And here are some examples of objects and written methods with syntax point:
- cat.purr()
- document.write()
- forms.elements.radio.click()
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
Objects, Properties and Methods in JavaScript.
Retrieved Dec 10, 2024, from
https://disenowebakus.net/en/objects-properties-methods-javascript