Respond to an unknown method call: Difference between revisions
Content added Content deleted
(→{{header|Object Pascal}}: Incorrect.) |
(→{{header|Kotlin}}: Changed code so that the object itself now captures an attempt to call an unknown method.) |
||
Line 591: | Line 591: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Kotlin JS does not currently target ECMAScript 2015 and so the Proxy object cannot be used for this task. The only way it can currently be accomplished is to use the Mozilla extension __noSuchMethod__ property which works with FireFox 43 but is no longer supported by more up to date versions: |
|||
{{incorrect|Kotlin|The object c is supposed to handle the situation, not the code site which tries to invoke foo on object c.}} |
|||
<lang scala>// Kotlin JS version 1. |
<lang scala>// Kotlin JS version 1.2.0 (FireFox 43) |
||
class C |
class C { |
||
// this method prevents a TypeError being thrown if an unknown method is called |
|||
fun __noSuchMethod__(id: String, args: Array<Any>) { |
|||
⚫ | |||
if (args.size > 0) println("which takes arguments: ${args.asList()}") |
|||
⚫ | |||
} |
|||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
val c: dynamic = C() // 'dynamic' turns off compile time checks |
val c: dynamic = C() // 'dynamic' turns off compile time checks |
||
⚫ | |||
try { |
|||
⚫ | |||
⚫ | |||
catch (t: Throwable) { |
|||
if (t.message == "undefined is not a function") { |
|||
⚫ | |||
} |
|||
else { |
|||
println(t.message) |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||