Created
May 2, 2020 15:13
-
-
Save chetanraj/c0a04293c2acf53b9aeb1395c81ccd53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Operation { | |
constructor(a, b) { | |
this.a = a | |
this.b = b | |
} | |
getSum() { | |
return this.a + this.b | |
} | |
} | |
let o = new Operation(2, 3); | |
o.getSum(); // 5 | |
o.getDiff(); // Output - Uncaught TypeError: o.getDiff is not a function | |
// Check if the function exists | |
typeof o.getDiff != "undefined" && o.getDiff(); | |
// With the use of optional chaining | |
o.getDiff?.() // Output - undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment