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

LESSON: js-object-constructors


Tags: javascript, objects


  • 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.


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


  • 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


  • Make a new constructor:
const Run = function (date, distance, timeTaken) { = 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)