Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary object methods and properties in JavaScript

I'm sure this has definitively been answered before, and I've tried to search for it.. maybe my search terms are wrong...

Basically I have an object myObject, and I have a set of defined properties and methods for it. What I want to do is be able to handle calls/references to properties and methods that I have not defined.

For example, let's say I have this:

var myObject = {
  someProperty : 'foobar',
  someFunction : function () { /* Do stuff */ }
}

Currently, if someone tries to make a call to myObject.someOtherFunction(), JavaScript yells and screams about it. What I want to do is setup a way to automatically handle that. So for example, instead of JavaScript throwing an error, my object just returns false. Is this possible?

Another way to look at it is this:

var myObject = {
  someFunction : function () { /* Do stuff */ }
  magicBucket : function () { /* Do stuff */ }
}

If I call myObject.someFunction(), well that is defined and does something. What I want to happen is if I were to for instance call myObject.someOtherFunction(), instead of JavaScript throwing an error, it would call myObject.magicBucket().

The reason is that I have a client that uses a third-party library on their site. They want to discontinue using it, but completely removing it is going to take a lot of time and effort. So as a short-term solution, they wanted to know if I could make a dummy file that basically does nothing. Well, this library uses several objects that has lots of methods. I could go through everything and make dummy objects, but I thought maybe there might be some easy "catch-all" method to do this.

Some have mentioned checking if the method exists first, wrapping it in a condition or try..catch, etc. Well, the point of this is that at this time I can't touch the actual calls to the methods. And since the overall goal is to eventually remove the coding altogether, it's not even applicable.

like image 713
slinkhi Avatar asked Aug 28 '13 14:08

slinkhi


People also ask

What are properties and methods for objects in JavaScript?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

What are the properties of object in JavaScript?

Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object.

What are arbitrary objects?

An arbitrary object has those properties common to the individual objects in its range. So an arbitrary number is odd or even, an arbitrary man is mortal, since each individual number is odd or even, each individual man is mortal.


1 Answers

There's a special property called __noSuchMethod__ which does precisely what you just described. However it's a non-standard property. It only works in Firefox. Here's how you use it:

var o = {
    __noSuchMethod__: function (name, args) {
        alert(name); // prints the name of the method
        alert(args); // prints the array of arguments
    }
};

o.abc(1, 2, 3); // OUTPUT: abc 1,2,3

The future however are proxy objects. The following is a short tutorial on proxies: Proxy Tutorial

like image 181
Aadit M Shah Avatar answered Sep 28 '22 01:09

Aadit M Shah