3
Preferences = {
 XDPI:90,
 YDPI:90,
 *:function(missing_name) {"Tell Joe he forgot to implement " + missing_name+ " property or func"}
}

Say I got an old/undocumented/minified/uglified class I want to replace with my own implementation. How could I catch all the old properties that could be missing from within my new "object" ?.

(Say there are a lot of client script (macros) used by non-technical users. I want to ease the report of missing func)

E.g if a script call Preferences.CurrentPrinter I want the Preferences object to diagnose it lacks a CurrentPrinter property without the user having to look at the console

Hans Z
  • 4,664
  • 2
  • 27
  • 50
frenchone
  • 1,547
  • 4
  • 19
  • 34
  • You can't do this in Javascript. You should be able to wrap running the client macros in a `try..catch` to provide error reporting. – millimoose Sep 11 '13 at 00:02
  • 1
    possible duplicate of [Modifying an object's prototype to made a log to the console every time a variable is requested](http://stackoverflow.com/questions/15650343/modifying-an-objects-prototype-to-made-a-log-to-the-console-every-time-a-variab). You might be looking for [Mozilla's nonstandard `__noSuchMethod__` handler](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod), or for the [`window.onerror` handler](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror) – Bergi Sep 11 '13 at 00:05
  • You can use ECMAScript 6 proxies, starting in 2016 or so. –  Sep 11 '13 at 01:10
  • 1
    possible duplicate of [Is there an equivalent of the \_\_noSuchMethod\_\_ feature for properties, or a way to implement it in JS?](http://stackoverflow.com/questions/2266789/is-there-an-equivalent-of-the-nosuchmethod-feature-for-properties-or-a-way) –  Sep 11 '13 at 01:12

3 Answers3

2

Sixth edition of ECMAScript specification introduces Proxy objects for that purpose:

http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

But this is not widely supported yet. At the moment of writting this, only Edge and Firefox browsers do that:

http://caniuse.com/#feat=proxy

P.S. Lucky you if you read that in future and all browsers already support that :)

Grief
  • 1,839
  • 1
  • 21
  • 40
1

You probably don't want to do something like that, having an object return undefined for properties that don't exist is something that gets relied on a lot.

What you probably should do is just check to see if your Preferences.member is undefined when you want that functionality instead of changing the way accessors work on your object.


If you insist, though what you could do is implement a method called get() that gets the property based on the string passed in and do all calls that way.

Preferences = {
   varX=90;
   varY=90;
   get = function(arg) {
       if(typeof this[arg] != 'undefined') {
           return this[arg];
       }
       Console.log("{0} not found in Preferences".format(arg));
   };
}

And then instead of doing Preferences.varX you do Preferences.get(varX).

Hans Z
  • 4,664
  • 2
  • 27
  • 50
  • My users are non-technical users so they won't be able to look at the console and more over they can't fix or even understand what's wrong in existent scripts (that could be obfuscated). So the only way to go is to crash violently but log what's gone wrong – frenchone Sep 11 '13 at 09:45
1

For methods you can use noSuchMethod, but it only works in Firefox https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod

You can get more informations from this post: Is there an equivalent of the __noSuchMethod__ feature for properties, or a way to implement it in JS?

Community
  • 1
  • 1
Saicoder
  • 11
  • 2