unbug

@unbug

Swift and JavaScript comparison snippets(8) — Classes

Swift

// class definition
class Counter {
var count = 0
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
// class instance
let counter = Counter()
// the initial count value is 0
counter.increment()
// the count's value is now 1
counter.increment(by: 5)
// the count's value is now 6
counter.reset()
// the count's value is now 0
print("The count property value is \(counter.count)")

JavaScript

// class definition
class Counter {
contructor() {
this.count = 0
}
function increment() {
this.count += 1
}
function increment(amount) {
this.count += amount
}
function reset() {
this.count = 0
}
}
// class instance
let counter = Counter()
// the initial count value is 0
counter.increment()
// the count's value is now 1
counter.increment(5)
// the count's value is now 6
counter.reset()
// the count's value is now 0
console.log(`The count property value is ${counter.count}`)

More of Swift and JavaScript comparison snippets

More by unbug

Topics of interest

More Related Stories