8

It is possible to create getters and setters in javascript as shown by

Object.defineProperty
__define***__

In all those instances, the name of the property is known.

Is it possible create a generic one.

By this I mean, I have a getter and or setter and it is called irrespective of the property name.

Is this possible? If so, how?

regards.

Note: I did find these after posting the question. Looks like it is currently not possible as the first answer stated.

Is it possible to implement dynamic getters/setters in JavaScript?

Monitor All JavaScript Object Properties (magic getters and setters)

Community
  • 1
  • 1
ritcoder
  • 3,274
  • 10
  • 42
  • 62

5 Answers5

9

To all time travelers like me:

It was not possible at the time the question was asked, but it is already possible in present versions of EcmaScript via so-called proxy objects. See more here:

jaboja
  • 2,178
  • 1
  • 21
  • 35
6

There is a non standard function __noSuchMethod__() which is executed when a non existing property is invoked as a function.

But I don't think there is exactly what you are looking for in JavaScript (yet).

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks. That is what I've also realized. Looks like I'll have to do it the old function way until it is implemented. – ritcoder Jan 27 '12 at 08:46
1

Not possible in standard javascript at this point.

smparkes
  • 13,807
  • 4
  • 36
  • 61
0

I feel like you guys were looking for something like this

function getterSetter()
{
var valA;
this.set=function (propName,val)
{
if(typeof this[propName] =='function' )
{
return false;
}
this[propName]=val;
}
this.get=function (propName,val)
{
if(typeof this[propName] =='function' )
{
return false;
}
return this[propName];
}
}

Here the set and get methods are setter and getter. You can verify this with following code.

var testObj=new getterSetter();
testObj.set('valA',10);
alert(testObj.get('valA'));

Also, checking for the propName to set/get is not a function.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
  • 1
    The idea is to be able to know when a request for a non existing property or method is made and to execute some custom function - much like the way dynamics work in c#. – ritcoder Oct 16 '12 at 01:40
0

I suppose you are expected to handle this yourself:

if (!object.hasOwnProperty('foo')) {
  // object has a foo property, its value might be undefined

} else if (typeof object.foo != 'undefined') {
  // there is a foo property elsewhere on object's prototye chain with 
  // a value other than undefined

} else {
  // the foo property might exist on the prototype chain with
  // a value of undefined, or might not exist on the chain at all
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 3
    I'm not really following. If I call obj.foo and foo is not defined, how will be code be called? I mean a full working example will be appreciated. – ritcoder Jan 27 '12 at 08:15
  • I mean that others have said "you can't do that" so I'm just adding that you should check that obj.foo exists before calling it. The above is working code of how to do that, however it won't tell you if `foo` is callable. There is a discussion about how to determine if something is callable on the [is Function](http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/13b14565817a35ba/b9e22a812194a9f3?hl=en&lnk=gst&q=is+callable#b9e22a812194a9f3) thread on comp.lang.javascript. – RobG Jan 30 '12 at 00:13
  • 1
    Ok. What I want to to do is to actually trap the calls to the missing properties and report them. It is needed as part of a project I'm working on. Because of that I dont actually know what properties will be called. – ritcoder Feb 17 '12 at 14:28