Created
February 5, 2020 17:16
-
-
Save martinlaws/410c1b0e79fc28e41ae08b033c7d1511 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(firstName, lastName, hairColor, heightInCm, favouriteAnimal = 'dog') { | |
this.firstName = firstName | |
this.lastName = lastName | |
this.hairColor = hairColor | |
this.heightInCm = heightInCm | |
this.heightInInches = function() { | |
return this.heightInCm / 2.54 | |
} | |
this.pets = [] | |
this.favouriteAnimal = favouriteAnimal | |
} | |
const martin = new Person('Martin', 'Laws', 'brown', 188) | |
function Pet(name, species, age, favouriteChewToy) { | |
this.name = name | |
this.species = species | |
this.age = age | |
this.favouriteChewToy = favouriteChewToy | |
this.bark = function() { | |
console.log(`woof woof I am ${this.name}`) | |
} | |
} | |
var maya = new Pet('Maya', 'dog', 4.5, 'pig') | |
var apples = new Pet('Apples', 'turtle', 33, 'fruit') | |
martin.pets.push(maya, apples) | |
console.log(martin) | |
martin.pets[0].bark() | |
martin.pets[1].bark() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = 'Martin' | |
var number = 123 | |
var alsoNumber = 3.14159 | |
var notANumber = NaN | |
var boolean = true | |
var array = [] | |
var notAThing = null | |
var nothingHere | |
var person = { | |
id: 1, | |
firstName: 'Martin', | |
lastName: 'Laws', | |
fullName() { | |
return `This user's full name is: ${this.firstName} ${this.lastName}` | |
}, | |
heightInCm: 188, | |
heightInInches() { | |
return this.heightInCm / 2.54 | |
}, | |
favouriteColours: ['blue', 'teal', 'grey'], | |
isBusy: true, | |
friends: [], | |
addFriend(newFriendName) { | |
if (typeof newFriendName === 'string' && !this.friends.find(friend => friend === newFriendName)) { | |
this.friends.push(newFriendName) | |
} | |
} | |
} | |
console.log(person) | |
person.addFriend('Scott') | |
person.addFriend('Scott') // this is ignored because we are checking for duplicates in addFriend() | |
person.addFriend(false) | |
person.addFriend(123) | |
person.addFriend(undefined) | |
console.log(person) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Pet(name, owner, species) { | |
this.name = name | |
this.owner = owner | |
this.species = species | |
this.birthday = Date.now() | |
this.speak = function() { | |
if (this.species === 'dog') { | |
console.log('bark bark') | |
} else if (this.species === 'cat') { | |
console.log('meow') | |
} else { | |
console.log('I think there has been a misunderstanding') | |
} | |
} | |
} | |
var pets = [] | |
function createPet(name, owner, species = 'dog') { | |
var newPet = new Pet(name, owner, species) | |
pets.push(newPet) | |
} | |
createPet('Hugo', 'Andy') | |
createPet('Maya', 'Martin') | |
createPet('Obie', 'Martin', 'cat') | |
createPet('Martin', 'Danielle', 'human') | |
console.table(pets) | |
pets[0].speak() | |
pets[2].speak() | |
pets[3].speak() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const martin = { | |
firstName: 'Martin', | |
heightInCm: 188 | |
} | |
Object.freeze(martin) | |
martin.firstName = 'Horatio' | |
martin.favouriteAnimal = 'dog' | |
console.log(martin) | |
let example = 'outside test' | |
{ | |
let example = 'inside test' | |
console.log(example) | |
} | |
console.log(example) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment