Skip to content

Instantly share code, notes, and snippets.

@jasenmichael
Created March 2, 2018 02:02
Show Gist options
  • Save jasenmichael/1bcb88fc6ad366fc1158f6adec6c3c2a to your computer and use it in GitHub Desktop.
Save jasenmichael/1bcb88fc6ad366fc1158f6adec6c3c2a to your computer and use it in GitHub Desktop.

JavaScript Object Constructors

try it on repl.it

// Constructor function to create "Person" objects
function Person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}

// Create a Person object
let someDude = new Person("John", "Doe", 50, "blue")
console.log(someDude)
// add a key value pair to the Person object
someDude.language = "English"
console.log(someDude)


console.log("_______________")


//  Manually create a "Place" object function
function Place(city, state, zip) {
  return {
    city,
    state,
    zip
  }
}

// Create a Place object
let shittyTown = Place('Florence', 'Kentucky', 41042)
console.log(shittyTown)
// add a key value pair to the Place object
shittyTown.isGhetto = true
console.log(shittyTown)


console.log(someDude.firstName + " is from " + shittyTown.city + ", " + shittyTown.state + ".")

links:

Mozilla MDN

W3Schools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment