Skip to content

Instantly share code, notes, and snippets.

@martinlaws
Created September 18, 2019 16:10
Show Gist options
  • Save martinlaws/dda72364782c84622d8bdd5cfae756d0 to your computer and use it in GitHub Desktop.
Save martinlaws/dda72364782c84622d8bdd5cfae756d0 to your computer and use it in GitHub Desktop.
const person = {
firstName: 'Martin',
lastName: 'Laws',
heightInCm: 188,
heightInInches: function() {
return this.heightInCm / 2.54
},
speak() {
console.log(`Hi, my name is ${this.firstName}!`)
return
},
friends: []
}
person.heightInInches()
function Person(firstName, lastName, heightInCm) {
this.firstName = firstName
this.lastName = lastName
this.heightInCm = heightInCm
this.friends = []
this.addFriend = function(friend) {
if (typeof friend === 'object') {
this.friends.push(friend)
} else {
console.log('GO GET A REAL FRIEND!')
}
}
this.fullName = function() {
console.log(`${this.firstName} ${this.lastName}`)
}
this.heightInInches = function() {
return this.heightInCm / 2.54
}
this.speak = function() {
console.log(`Hi, my name is ${this.firstName}!`)
return
}
}
const martin = new Person('Martin', 'Laws', 188)
const scott = new Person('Scott', 'Morrison', 189)
martin.addFriend(scott)
martin.addFriend(123)
// scott.friends.push(martin)
console.log('Martin:', martin)
console.log('Scott:', scott)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment