Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define getter on object so all undefined property lookups return ""

Basically I need to be able to do this:

var obj = {"foo":"bar"},
    arr = [];
with( obj ){
   arr.push( foo );
   arr.push( notDefinedOnObj ); // fails with 'ReferenceError: notDefinedOnObj is not defined'
}
console.log(arr); // ["bar", ""] <- this is what it should be.

I'm looking for a "global" equivalent of {}.__defineGetter__ or {get} in order to return an empty string for all undefined property getters (note that this is different than a property that is undefined).

like image 447
David Murdoch Avatar asked May 23 '12 21:05

David Murdoch


People also ask

What does .get do in JavaScript?

get() method in JavaScript is used to allow users to get the property from an object as a function. This method always returns the value of the property.

Why do we use getter and setter in JavaScript?

In JavaScript, accessor properties are methods that get or set the value of an object. For that, we use these two keywords: get - to define a getter method to get the property value. set - to define a setter method to set the property value.

What is a getter method in JavaScript?

Getters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed.

What are classes What are getters and setters in JavaScript?

In a JavaScript class, getters and setters are used to get or set the properties values. “get” is the keyword utilized to define a getter method for retrieving the property value, whereas “set” defines a setter method for changing the value of a specific property.


Video Answer


1 Answers

You can create a Proxy to return an empty string whenever undefined properties are accessed.

app.js:

var obj = {"foo":"bar"},
    arr = [],
    p = Proxy.create({
        get: function(proxy, name) {
            return obj[name] === undefined ? '' : obj[name];
        }
    });
arr.push( p.foo );
arr.push( p.notDefinedOnObj );

console.log(arr);

As question author David Murdoch notes, if you are using node v0.6.18 (the latest stable release at the time this post was written), you must pass the --harmony_proxies option when you run the script:

$ node --harmony_proxies app.js
[ 'bar', '' ]

Note that this solution will not work if you use with, as in:

var obj = {"foo":"bar"},
    arr = [],
    p = Proxy.create({
        get: function(proxy, name) {
            return obj[name] === undefined ? '' : obj[name];
        }
    });
with ( p ) {
   arr.push( foo ); // ReferenceError: foo is not defined
   arr.push( notDefinedOnObj );
}

console.log(arr);

with does not seem to call the proxy's get method when adding the proxy to the scope chain.

Note: the proxy handler passed to Proxy.create() in this is example is incomplete. See Proxy: Common mistakes and misunderstanding for more details.

like image 101
thisgeek Avatar answered Sep 18 '22 01:09

thisgeek