home..

Javascript Object Oriented Programming

Javascript class

Purpose of object oriented programming

Making a class

%%js

class Cat{
    constructor(furColor, weight, breed, sound){
        // 'this' here is used to say that this attribute belongs to this class
        this.furColor = furColor;
        this.weight = weight;
        this.breed = breed;
        this.sound = sound;
    }
    
    makeSound() {
        console.log(this.sound);
        // Additional code to make sound goes here
    }
    displayBreed() {
        console.log(this.breed);
    }

}

// define the variables we will pass into our cat class
var furColor = "Black";
var weight = 9;
var breed = "Bombay"
var sound = "meow"

// to make a new cat you need to call new and pass 
// all of the variables that the cat constructor is expecting
var cat = new Cat(furColor, weight, breed, sound);

// call a class function
cat.makeSound();
cat.displayBreed();
<IPython.core.display.Javascript object>
%%js
// Make a small class that could relate to your passion project here

Extending a class

%%js

class Pet{
    constructor(name, weight, breed){
        this.name = name;
        this.weight = weight;
        this.breed = breed;
    }

    print() {
        console.log(this.name, 
                    this.weight, 
                    this.breed, 
                    this.getSound());
    }
    
    getSound() {
        return "none";
        // Additional code to make sound goes here
    }
}


// use the extends keyword to say what the parent class of this class is
class Cat extends Pet{
    constructor(name, weight, breed, sound){
        // use super to call the parent constructor here
        super(name, weight, breed);
        this.sound = sound;
    }

    getSound() {
        return this.sound;
        // Additional code to make sound goes here
    }
}

class Dog extends Pet{
    constructor(name, weight, breed, sound){
        super(name, weight, breed);
        this.sound = sound;
    }

    getSound() {
        return this.sound;
        // Additional code to make sound goes here
    }
}

class Shark extends Pet{
    constructor(name, weight, breed, sound){
        super(name, weight, breed);
        this.sound = sound;
    }

    getSound() {
        return this.sound;
        // Additional code to make sound goes here
    }
}

// define the variables we will pass into our pet class
var fishName = "Goldy";
var fishWeight = 0.1;
var fishBreed = "Gold Fish";
// construct fish
var fish = new Pet(fishName, fishWeight, fishBreed);

// define the variables we will pass into our cat class
var catName = "Ziggy";
var catWeight = 9;
var catBreed = "Bombay";
var catSound = "Meow";
// construct cat
var cat = new Cat(catName, catWeight, catBreed, catSound);

var sharkName = "Fluffy";
var sharkWeight = 20;
var sharkBreed = "Shark";
var sharkSound = "Chomp!";

// define the variables we will pass into our dog class
var dogName = "Fido";
var dogWeight = 50;
var dogBreed = "Mutt";
var dogSound = "Bark";
// construct dog
var dog = new Dog(dogName, dogWeight, dogBreed, dogSound);
var shark = new Shark(sharkName, sharkWeight, sharkBreed, sharkSound);

var animals = [fish, cat, dog, shark]; // Use an array to store the animals
for (var animal of animals) {
    animal.print();
}
<IPython.core.display.Javascript object>
%%js
// Make a parent class and a child class related to your passion project

Draw.io


Draw_io_example

draw_io_example.png

Hacks

© 2024    •  Powered by Soopr   •  Theme  Moonwalk