Categories
JavaScript Web development

JavaScript Objects

In JavaScript, objects are king. If you understand objects, you understand JavaScript. All JavaScript values (except primitives) are objects.

webdev gif
Do you like this gif? Check more gifs in the store.

JavaScript Primitives:

primitive value is a value that has no properties or methods.

primitive data type is data that has a primitive value.

JavaScript defines 5 types of primitive data types:

  1. string
  2. number
  3. boolean
  4. null
  5. undefined

Remember: Do Not Declare Strings, Numbers, and Booleans as Objects!

Real Life Objects, Properties, and Methods

In real life, a car is an object.

A car has properties like weight and color, and methods like start and stop:

ObjectPropertiesMethods
Minicar.name = Minicar.start()
car.model = 3doorscar.drive()
car.weight = 900kgcar.brake() 
car.color = redcar.stop()

All cars have the same properties, but the property values differ from car to car.

All cars have the same methods, but the methods are performed at different times.

JavaScript Objects

You have already learned that JavaScript variables are containers for data values (to read more about JavaScriot Variables click here).

This code assigns a simple value (Mini) to a variable named car:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo"></p>

<script>
var car = "Mini";
document.getElementById("demo").innerHTML = car;
</script>

</body>
</html>

Output:

Objects are variables too. But objects can contain many values.

This code assigns many values (Mini, 3doors, red) to a variable named car:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p id="demo"></p>

<script>

var car = {type:"Mini", model:"3doors", color:"red"};

document.getElementById("demo").innerHTML = "The car type is " + car.type;
</script>

</body>
</html>

Output:

The values are written as name:value pairs (name and value separated by a colon).

JavaScript objects are containers for named values called properties or methods.

Object Definition

You define (and create) a JavaScript object with an object literal:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p id="demo"></p>

<script>
var person = {firstName:"Adam", lastName:"Smith", age:48, eyeColor:"green"};

document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>

</body>
</html>

Output:

Object Properties

The name:values pairs in JavaScript objects are called properties:

PropertyProperty Value
firstNameAdam
lastNameSmith
age48
eyeColorgreen

Object Methods

Objects can also have methods.

Methods are actions that can be performed on objects.

Methods are stored in properties as function definitions.

PropertyProperty Value
firstNameAdam
lastNameSmith
age48
eyeColorgreen
fullNamefunction() {return this.firstName + ” ” + this.lastName;}

Example:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p>An object method is a function definition, stored as a property value.</p>

<p id="demo"></p>

<script>

var person = {
  firstName: "Adam",
  lastName : "Smith",
  id     : 1122,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

document.getElementById("demo").innerHTML = person.fullName();
</script>

</body>
</html>

Output:

The this Keyword

In a function definition, this refers to the “owner” of the function.

In the example above, this is the person object that “owns” the fullName function.

In other words, this.firstName means the first.Name property of this object.

Enjoy coding!

Leave a Reply