JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Practice
  • Bonus Challenge
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 10 Objects

Constructor Functions

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 11-Objects
                01-properties.js
                02-enumeration.js
                03-initializers.js 
                04-constructor-function.js <----You will be working in this file.

You've seen a few times that objects can be created from models. The actual name for this is a "constructor function"; literally, a function that constructs an object. You will be using these quite often in the future, especially once you reach React.js and the Angular framework. For now, just another review.

function Person(name, age){
    //Note that the argument aligns with the RHS(Right Hand Side) 
    //The variable created will take the place of this
    this.name = name;
    this.age = age;
}

var paul = new Person("Paul", 41);
console.log(paul.name);

var sophie = new Person("Sophie", 5);
var ava = new Person("Ava", 15);

console.log(sophie.name);

Practice

  1. Re-create the people objects from the previous module, but this time use a constructor function. Then put them in an array.

  2. Print the properties of each person to the console. However, this time iterate through the array and group the properties together by type, like this:

Age: Ted - 20
Age: Paul - 41
Age: Aaron - 30
Eye Color: Ted - brown
Eye Color: Paul - brown
Eye Color: Aaron - black
etc...

Bonus Challenge

Come up with another challenge or two on your own based around objects. Slack them to the instructor.

PreviousInitializersNextthis

Last updated 7 years ago