Swift and JavaScript comparison snippets(8) — Classes

Written by unbug | Published 2018/07/03
Tech Story Tags: javascript | swift | swift-and-javascript | swift-vs-javascript

TLDRvia the TL;DR App

Swift

// class definitionclass Counter {var count = 0func increment() {count += 1}func increment(by amount: Int) {count += amount}func reset() {count = 0}}

// class instancelet counter = Counter()// the initial count value is 0counter.increment()// the count's value is now 1counter.increment(by: 5)// the count's value is now 6counter.reset()// the count's value is now 0

print("The count property value is \(counter.count)")

JavaScript

// class definitionclass Counter {contructor() {this.count = 0}function increment() {this.count += 1}function increment(amount) {this.count += amount}function reset() {this.count = 0}}

// class instancelet counter = Counter()// the initial count value is 0counter.increment()// the count's value is now 1counter.increment(5)// the count's value is now 6counter.reset()// the count's value is now 0

console.log(`The count property value is ${counter.count}`)

More of Swift and JavaScript comparison snippets


Published by HackerNoon on 2018/07/03