reading-notes

Aloysious' Codefellows Reading Notes

View the Project on GitHub

Code 201 Reading Notes

Class 7

Class 1 Instructor’s Repo

<== Previous Lesson Next Lesson ==>

<== Home 🏠

Intro to Object-Oriented Programming with Constructor Functions; HTML Tables

DomainModeling

Tables HTML book (pp.126-145)

Functions JS Book (pp.86-99 review)

Objects JS Book (pp.100-128)

In real life, a car is an object.

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

Object Properties Methods

car.name = Fiat

car.model = 500

car.weight = 850kg

car.color = white

car.start()

car.drive()

car.brake()

car.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 variables are containers for data values.

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

var car = "Fiat";

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

This code assigns many values (Fiat, 500, white) to a variable named car:

var car = {type:"Fiat", model:"500", color:"white"};

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:

Example

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

Spaces and line breaks are not important. An object definition can span multiple lines:

Example

var person = {

firstName: “John”,

lastName: “Doe”,

age: 50,

eyeColor: “blue”

};

Object Properties

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

Property Property Value

firstName John

lastName Doe

age 50

eyeColor blue

Accessing Object Properties

You can access object properties in two ways:

objectName.propertyName or objectName["propertyName"]

Objects CW3 Schools

Methods JS Book (pp.106-144)

this tutorial JavaScript

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 firstName property of this object.

JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

Methods CW3 Schools

From the Duckett HTML book:

From the Duckett JS Book:

<== Previous Lesson Next Lesson ==>

<== Home 🏠