10

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)

5 Answers 5

10

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:

Sign up to request clarification or add additional context in comments.

Comments

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).

1 Comment

Thanks. That is what I've also realized. Looks like I'll have to do it the old function way until it is implemented.
1

Not possible in standard javascript at this point.

Comments

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
}

3 Comments

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.
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 thread on comp.lang.javascript.
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.
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.

1 Comment

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#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.