Classes
methods, instantiating objects, using objects, calling methods, parameters, return values.
Methods
length() – Returns the length of a string.
indexOf() – Returns the index of a specified character in a string.
charAt() – Returns the character at a specified index in a string.
substring() – Returns a substring of a specified string.
toUpperCase() – Converts a string to uppercase.
toLowerCase() – Converts a string to lowercase.
trim() – Removes leading and trailing whitespace from a string.
Some methods can also be ussed on arrays making them very usefull for creating conditionals like a loop that goes through every value, find a value in an array and much more. In adventure game when creating the quest system the most comon method I used was length and index of.
%%js
let array = [17, 22, 35, 41, 58];
console.log(array.length);
console.log(array[3]);
<IPython.core.display.Javascript object>
Instantiating Objects
instantiating objects is when you create A json object that can be used very easily in OOP. To instance an object you need to use the class function which is like a JSON Object but it can store data,function, and more and not just data
%%js
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
displayInfo() {
return `${this.make} ${this.model} (${this.year})`;
}
age(currentYear) {
return currentYear - this.year;
}
}
let hotWheel = new Car("Cachowww", "Lightning McQueen", 2006);
console.log(hotWheel.displayInfo());
console.log(`Age of the car: ${hotWheel.age(2023)} years`);
<IPython.core.display.Javascript object>
Using Objects
To use an object you need to use the “new” keyword to create a new instance of the object. This new intance of the object will have its own values and will not share its values with the other objects that is also in the same class. After calling new choose the class you want to use and give the values in the (). remmeber that you cant skip values and have to go in order!
Calling Methods
Calling methods are functions that are inside of the object that can return values of the object. thi can be used to find an objects speed,image,size, and anything else you want to display! to call one of these methods you must use the object (which is inside of the variable you made after calling the class) and use the “.” folowed by the name of the function to call the method
%%js
class Boat{
constructor(maxSpeed, Size, Color){
this.maxSpeed = maxSpeed;
this.Size = Size;
this.Color = Color;
this.Fuel = 100;
}
drive(){
return `The boat is driving at ${this.maxSpeed} knots`;
this.Fuel -= 10;
}
tankLeft(){
return this.Fuel;
}
}
let coolBoat = new Boat(100, "Big", "Blue");
console.log(coolBoat.drive());
console.log(`Fuel left: ${coolBoat.tankLeft()}`);
Parameters
Parameters are values that you give to the object that is used to set values inside of the object. In the example above you provide the parameters for the max Speed, size, and color for the object. You add these parameters inside of the () as seen when creating the var coolBoat
Return Values
Return values return values about the object. In the example above I have a return value that returns the amount of fuel left isnide of the tank for the boat. If you wanted to add another return value to practice try to add a return value that calculates the amount of distance you can travel with the current fuel amount