How can a "undefined not function" be more useful?

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, since MyClass does not have a foo method, so MyClass.foo really returns undefined , and new does not like it. The problem is that this does not mean that the user may mistakenly name 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 has no method before new gets the undefined passed to it automatically? __noSuchMethod__ is exactly what I'm looking for, but it looks like it's a non-standard extension. It seems that IE does not support it and V8 refused to implement it . The Chromium team also does not care about proxy server support .

There seems to be some Proxy support for Node (see this and this ) and in the form of shim ("a polyfill for the upcoming ECMAScript Reflect API, but see the mpm comments below).

Related questions (what it boils down to):

+6
source share
1 answer

More than a year after this question, V8 probably changed the error messages

 var MyClass = function() { this.Foo = function() { console.log("Foo"); } } new MyClass().foo(); 

Throws this error today

Uncaught TypeError: (intermediate value) .foo is not a function

Used with a named object

 var mc = new MyClass(); mc.foo(); 

This is even more useful.

Uncaught TypeError: mc.foo is not a function

Used with typo in the object name is also useful:

 new myClass().foo(); 

Unprepared ReferenceError: myClass not defined

+3
source

All Articles