All of the programming objects group collections of properties and functions into Objects. Javascript is no different. It has its native language objects and it gives us the ability to create our own custom objects. We can create one just as any other variable and adding values within curly brackets. An object in Javascript is created like this:
| Code: |
<script type="text/javascript">
var myObject = {};
</script> |
To add Properties and Methods; they must go inside the curly brackets like this:
| Code: |
<script type="text/javascript">
var myObject = {
name: "jorge", talk: function (){ alert("hello"); }
};
</script> |
In the above I have declared an object called myObject with a property called name and a function called talk. In order to run the method it will be like this.
| Code: |
<script type="text/javascript">
myObject.talk();
</script> |
It should show an alert box with the word hello.