Swift and JavaScript comparison snippets - Inheritance
unbug
Posted on August 30, 2019
GitHub: https://github.com/unbug/sj
Inheritance
Swift
// Defining a Base Class
class Vehicle {
var currentSpeed = 0.0
var description: String {
return "traveling at \(currentSpeed) miles per hour"
}
func makeNoise() {
// do nothing - an arbitrary vehicle doesn't necessarily make a noise
}
}
let someVehicle = Vehicle()
print("Vehicle: \(someVehicle.description)")
// Vehicle: traveling at 0.0 miles per hour
// Subclassing
class SomeSubclass: SomeSuperclass {
// subclass definition goes here
}
class Bicycle: Vehicle {
var hasBasket = false
}
let bicycle = Bicycle()
bicycle.hasBasket = true
bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)")
// Bicycle: traveling at 15.0 miles per hour
// Subclasses can themselves be subclassed
class Tandem: Bicycle {
var currentNumberOfPassengers = 0
}
let tandem = Tandem()
tandem.hasBasket = true
tandem.currentNumberOfPassengers = 2
tandem.currentSpeed = 22.0
print("Tandem: \(tandem.description)")
// Tandem: traveling at 22.0 miles per hour
// Overriding
class Train: Vehicle {
override func makeNoise() {
print("Choo Choo")
}
}
let train = Train()
train.makeNoise()
// Prints "Choo Choo"
// Overriding Property Getters and Setters
class Car: Vehicle {
var gear = 1
override var description: String {
return super.description + " in gear \(gear)"
}
}
let car = Car()
car.currentSpeed = 25.0
car.gear = 3
print("Car: \(car.description)")
// Car: traveling at 25.0 miles per hour in gear 3
JavaScript
// Defining a Base Class
class Vehicle {
constructor() {
this.currentSpeed = 0.0
}
get description() {
return `traveling at ${currentSpeed} miles per hour`
}
function makeNoise() {
// do nothing - an arbitrary vehicle doesn't necessarily make a noise
}
}
let someVehicle = Vehicle()
console.log(`Vehicle: ${someVehicle.description}`)
// Vehicle: traveling at 0.0 miles per hour
// Subclassing
class SomeSubclass extends SomeSuperclass {
contructor() {
super();
}
// subclass definition goes here
}
class Bicycle extends Vehicle {
contructor() {
super();
this.hasBasket = false
}
}
let bicycle = Bicycle()
bicycle.hasBasket = true
bicycle.currentSpeed = 15.0
console.log(`Bicycle: ${bicycle.description}`)
// Bicycle: traveling at 15.0 miles per hour
// Subclasses can themselves be subclassed
class Tandem extends Bicycle {
contructor() {
super();
this.currentNumberOfPassengers = 0
}
}
let tandem = Tandem()
tandem.hasBasket = true
tandem.currentNumberOfPassengers = 2
tandem.currentSpeed = 22.0
console.log("Tandem: \(tandem.description)")
// Tandem: traveling at 22.0 miles per hour
// Overriding
class Train extends Vehicle {
contructor() {
super();
this.currentNumberOfPassengers = 0
}
function makeNoise() {
console.log("Choo Choo")
}
}
let train = Train()
train.makeNoise()
// Prints "Choo Choo"
// Overriding Property Getters and Setters
class Car extends Vehicle {
contructor() {
super();
this.gear = 1
}
get description() {
return `${super.description} in gear ${gear}`
}
}
let car = Car()
car.currentSpeed = 25.0
car.gear = 3
console.log(`Car: ${car.description}`)
// Car: traveling at 25.0 miles per hour in gear 3
💖 💪 🙅 🚩
unbug
Posted on August 30, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.