What are Objects?
Let”s take a look at a predefined object. Clear out your script and add this bit of code.
document.write( 'Hello World!' );
Lastly, you type the name of the property or method you want to use. In our case, we want to access the write() method. You’ll notice it’s in the same fashion as a function.
This is because methods are functions. So, what’s the difference? The term method is the technical term for when you’re talking about functions inside an object. If a function has no relation to an object, then it’s just considered a function.
Same thing goes for properties. Properties are variables. Properties is the technical term for when you’re talking about variables inside an object. If a variable is created inside an object, then you would call it a property and not a variable. They’re both the same thing, but different concepts.
If we were to do something like this.
write( 'Hello World!' );
We would get an error because we haven’t defined a function named write(). However, the object document contains a method named write(). If you refresh your page, you should see your message appear in the document of your browser.
Let’s go over how to access a property really quick. Clear out your script and add this bit of code.
var text = 'Hello World!';
alert( text.length );
We have a variable named text. We then alert out it’s length. A string is considered an object. You can access different properties and methods of the String object. In our case, we want to access the property length. The length is basically how many characters are inside the string. You should get alerted 12.