EXERCISE THREE: Make an Object Literal | Simple Javascript Project Training Tutorial

Опубликовано: 29 Ноябрь 2016
на канале: Powerslacker
2,233
17

FULL CODE + IMAGES + SLIDES @ http://blog.breakthru.solutions/learn...

PLAYLIST:    • Learn Javascript with a SIMPLE Projec...  

FOLLOW ME ON TWITTER:   / thepowerslacker  

FURTHER READING
The Basics of Object Oriented JS: https://code.tutsplus.com/tutorials/t...

FROM THE READING
Literal is a preferred option for name spacing so that your JavaScript code doesn't interfere (or vice versa) with other scripts running on the page and also if you are using this object as a single object and not requiring more than one instance of the object, whereas Constructor function type notation is preferred if you need to do some initial work before the object is created or require multiple instances of the object where each instance can be changed during the lifetime of the script. Let's continue to build on both our objects simultaneously so we can observe what the differences are.

Now is probably as good a time as any to explain how to use properties and methods (although you would already have done so if you are familiar with a library).

To use a property first you type what object it belongs to - so in this case it's myObject - and then to reference its internal properties, you put a full stop and then the name of the property so it will eventually look like myObject.iAm (this will return 'an object').

For methods, it is the same except to execute the method, as with any function, you must put parenthesis after it; otherwise you will just be returning a reference to the function and not what the function actually returns. So it will look like myObject.whatAmI() (this will alert 'I am an object').

Now for the differences:

The constructor object has its properties and methods defined with the keyword 'this' in front of it, whereas the literal version does not.
In the constructor object the properties/methods have their 'values' defined after an equal sign '=' whereas in the literal version, they are defined after a colon ':'.
The constructor function can have (optional) semi-colons ';' at the end of each property/method declaration whereas in the literal version if you have more than one property or method, they MUST be separated with a comma ',', and they CANNOT have semi-colons after them, otherwise JavaScript will return an error.
There is also a difference between the way these two types of object declarations are used.

To use a literally notated object, you simply use it by referencing its variable name, so wherever it is required you call it by typing;


Смотрите видео EXERCISE THREE: Make an Object Literal | Simple Javascript Project Training Tutorial онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Powerslacker 29 Ноябрь 2016, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,233 раз и оно понравилось 17 людям.