Consider this JavaScript code:
var x = new date()
// "ReferenceError: date is not defined" - useful error, hints at a typo ('D'ate)
var x = new MyClass.foo()
// "TypeError: undefined is not a function" - bad error, no hint it might be 'F'oo
The error itself is correct, because MyClass
doesn't have a foo
method, so MyClass.foo
does indeed return undefined
, and new
doesn't like that. The problem is this doesn't hint at all that the user might have misspelled the method name. Real-life example:
new Meteor.collection('foo') // instead of:
new Meteor.Collection('foo') // c and C look alike with many fonts at small size
Is there a portable way to detect that an object doesn't have a method before new
gets undefined
passed to it, automatically? __noSuchMethod__
is exactly what I'm looking for, but it looks like it's a non-standard extension. Looks like IE doesn't support it and V8 refused to implement it. The Chromium team also doesn't care much about implementing Proxy support.
Looks like there is some support for Proxy
for Node (see this and this) and in the form of a shim ("a polyfill for the upcoming ECMAScript Reflect API" but see mpm's comments below).
Related questions (what this one boils down to):