Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md

README.md

LESSON: js-object-constructors


2018-06-11

Tags: javascript, objects

OBJECTIVES

  • Use a constructor function to produce objects of a particular type.
  • Attach attributes to a new object using the constructor function.
  • Recall the cost of defining methods inside a constructor function.

VOCABULARY

Constructor function: used to make new objects with the same properties

  • Always start with capital letter
  • Use the keyword new
  • Used to reduce duplication

Instance: an object made from a constructor function

NOTES

  • Constructors:
    • Make the same type of objects again
    • Reduces duplicate code!
    • Duplicate code increases typos and errors and makes code less readable
  • Attributes with an underscore (ie this._power)
    • Indicates to other developers that you don’t intend this property to be directly called

EXAMPLES

  • Make a new constructor:
const Run = function (date, distance, timeTaken) {
    this.date = date
    this.distance = distance
    this.timeTaken = timeTaken
}
  • Make a new instance of the object using the constructor:
const currentRun = new Run ('2018-06-11', 1300, 700)