Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active May 21, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisonmalone/77df717325f24af4cf552c141c539567 to your computer and use it in GitHub Desktop.
Save harrisonmalone/77df717325f24af4cf552c141c539567 to your computer and use it in GitHub Desktop.
code from lecture on class and objects in javascript
// car class
class Car {
// just like ruby initialize
constructor(color, brand, type) {
this.color = color
this.brand = brand
this.type = type
Car.carCounter()
}
// instance method just like ruby
startCar() {
return 'vrroommmmm car starting up'
}
// this is a class method
static carCounter() {
return this.carCount += 1
}
static totalCars() {
return this.carCount
}
}
// creating class variable (also called instance property in javascript)
Car.carCount = 0
// instance of Car
const holden = new Car("purple", "holden", "petrol")
// invoking instance method
console.log(holden.startCar())
// invoking class method
const tesla = new Car("blue", "tesla", "electric")
const result = Car.totalCars()
console.log(result)
# car class
class Car
def initialize(color, brand, type)
@color = color
@brand = brand
@type = type
end
def start_car
return "vrrroommmmmmmm"
end
end
holden = Car.new("purple", "holden", "petrol")
p holden
// challenge #1
// - using the constructor function syntax create 4 different SocialMedia objects with name, yearFounded and totalUsers as attributes
// - write a method thats console.logs all the information about the social media object in this format "${name} was founded in ${year} and has a total user base of ${total_users}"
// challenge #2
// - create a rectangle class, it has one property (sides) which is an array of length by width
// - write an instance method to get the perimeter of rectangle
// - write an instance method to get the area of a rectangle
// challenge #3
// // Pagination Class
// // Create a class that will handle content pagination.
// // The class should take 2 parameters:
// // 1. items (default: []): An array of the contents to paginate
// // 2. pageSize (default: 10): Number of items to show on each page
// // You will have to implement the following methods:
// // prevPage <- Turn to previous page. If already at first page, notify user
// // nextPage <- Turn to next page. If already at last page, notify user
// // firstPage <- Go to first page
// // lastPage <- Go to last page
// // goToPage <- Takes an integer as input and goes to that page. If page doesn't exist, stay on current page, and notify user that page doesn't exist
// // getVisibleItems <- Display all items on current page (this will be an array of length pageSize)
// // Note: these methods must be chainable -> p.nextPage().nextPage()
// const items = "abcdefghijklmnopqrstuvwxyz".split("");
// pageSize = 5;
// const items1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
// // Write your class here
// const page = new Pagination(items, pageSize);
// console.log(p.getVisibleItems());
// // ["a", "b", "c", "d", "e"]
// p.nextPage();
// console.log(p.getVisibleItems());
// // ["f", "g", "h", "i", "j"]
// p.goToPage(7);
// // "I'm sorry, page 7 does not exist"
// p.lastPage();
// p.nextPage();
// // "You are already on the last page!"
// p.previousPage();
// console.log(p.getVisibleItems());
// // ["u", "v", "w", "x", "y"]
// const p1 = new Pagination(items1);
// console.log(p1.getVisibleItems());
// // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// p1.previousPage();
// // "You are already on the first page!"
// p1.firstPage();
// p1.nextPage();
// console.log(p1.getVisibleItems());
// // [11, 12, 13, 14, 15]
// p1.nextPage();
// // "You are already on the last page!"
// console.log(p1.getVisibleItems());
// // [11, 12, 13, 14, 15]
// challenge #4
// - create a tweet class, it has one property (tweet_content) which is a string
// - write an instance method to count how many characters are in a tweet
// - write an instance method to warn about spoilers for game of thrones... say that if a tweet that has "game", "thrones" or "jon snow" in it then return "spoilers" else return "no spoilers"
// - write an instance method to add a location and a user object (with a username and an @ handle) to a single tweet
// - write a static method that counts total tweets
// - create a class to handle errors (HandleError), in it have a static method that checks if characters are less than 140 characters (then don't create the tweet instance) otherwise create a tweet instance
// object literal
const user = {
name: 'harrison',
age: 26
}
// console.log(user)
// object constructor
const newUser = new Object()
newUser.name = "aaron"
// console.log(newUser)
// `this` represents the anotherUser object when called inside of a function
let anotherUser = {
name: 'harrison',
age: 26,
printNameAndAge: function() {
console.log(this.name)
}
}
// another syntax for functions inside of objects
let anotherUserDiffSyntax = {
name: 'harrison',
age: 26,
printNameAndAge() {
console.log(this.name)
}
}
// anotherUserDiffSyntax.printNameAndAge()
// anotherUser.printNameAndAge()
// constructor function
function User(name, age) {
// @name = name
// console.log(this)
this.name = name
this.age = age
this.printName = function() {
console.log(`hi my name is ${this.name} and i'm ${this.age}`)
}
}
// this is the js constructor function way
const harrison = new User('harrison', 26)
// harrison.printName()
const aaron = new User('aaron')
// console.log(aaron)
// this is the ruby way
// User.new('harrison', 26)
// factory functions
function City(name, population) {
return {
name: name,
population: population
}
}
// we don't use this to assign instance variables
const melbourne = City("melbourne", 4000000)
console.log(melbourne)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment