# Object literals

  • An object literal will be used to combine several related variables into one variable
  • This makes your code more readable, easier to maintain and more structured
  • Example:
  • Summarised:
    • An object literal is a standalone, comma-separated list of name-value pairs, wrapped inside curly braces
    • The value of an object variable (or property) can be a string, a number, a boolean, an array, another object, ...
    • An object can also contain functions (or methods)

REMARK

  • Object literals are standalone objects and are NOT instantiated from a class (classes this will be discussed in a later section)

# Syntax

  • Properties inside an object can be written as:
property description
name: 'John' without quotes (recommended)
'name': 'John' with single quotes
"name": 'John' with double quotes
  • Functions (or methods) inside an object can be written as:
function/method description
greet() {} ES6 shorthand (recommended)
greet: function() {} as a name-value pair

# Accessing properties and methods

# Outside the object

  • All properties and methods can be accessed with the dot notation
  • In our previous example:
access description
user.name get the name value of the user object
user.name = 'Jane set (overwrite) the name value of the user object
user.greet() run the greet() function of the user object
  • It's also possible to add and remove properties to/from the object
access description
user.role = 'admin' add the property role to the user object
delete user.age delete the age property from the user object

# Inside the object

  • Functions inside the object can access the properties with the this keyword
    • Because we use "normal" functions inside the object (not arrow functions), the this keyword refers to the object itself and not the global window object

 


greet() {
    console.log(`Hi, ${this.name} ${this.surname}`);
}
1
2
3

WARNINGS

  • Always use "normal" functions inside an object because the this keyword then refers to the object itself
  • It's not a good idea to use arrow functions inside objects, because the this keyword inside arrow functions refers to the global window object which makes it (much) harder to access the embedded properties

# JSON.stringify()

  • When writing an object directly to the DOM, you get a strange result:
document.querySelector('pre').textContent = user;
// result on page: [object Object]
1
2
  • JSON.stringify() converts all properties (not the functions/methods!) of the object to a human-readable string
document.querySelector('pre').textContent = JSON.stringify(user);
// result on page: {"name":"John","surname":"Doe","age":41}
1
2

# Examples

# Basic object literal

  • Open es6/objects/literal.html and es6/objects/literal.js
  • This is the basic example we discussed above

# Campus info

  • Open es6/objects/campus.html and es6/objects/campus.js
  • In this example we want to show a map of our Thomas More campus at Geel
    • For the map we use a static generated image from Yandex.com, one of the few static maps generators that don't require an API key
  • Let's take a closer look at the Yandex API documentation (opens new window) on how to generate a static map with a marker:
    • The url to the image is: https://static-maps.yandex.ru/1.x/?{URL parameters}
    • The URL parameters we need for our map are:

# Geolocation API (OPTIONAL)

  • Objects and object literals are found pretty much everywhere in JavaScript
  • Also, JavaScripts build-in Web API's (opens new window) usually give you the information in the form of an object
  • In this example, we use the build-in Geolocation API (opens new window) that give us our exact location
  • The Geolocation API is available through the navigator.geolocation object and has tree methodes:
methode description
navigator.geolocation.getCurrentPosition(success[, error[, options]]) (opens new window) get the current position (on time sample)
navigator.geolocation.watchPosition(success[, error[, options]]) (opens new window) get the new position every time you move
navigator.geolocation.clearWatch(id) (opens new window) stop watching your position

WARNING

  • The Geolocation API only works in a secure environment (https://), except for development (http://localhost)
  • The user must authorise you the share his location
  • Open es6/objects/geolocation.html and es6/objects/geolocation.js
  • In this example we use the watchPosition() (opens new window) methode which has three parameters:
    • success: a callback function that contains the measured position in the form of an object
    • error (optional): a callback function that contains an error object (e.g: GPS is off, timeout has expired, ...)
    • options (optional): some options like the accuracy for the measurement, and the timeout after which the error is triggered
Last Updated: 4/27/2021, 10:40:15 PM