admin管理员组

文章数量:1026989

I'd like to set a default return value for an object.

If I have a set up like in the moz docs

var o = { a: 0 };

Object.defineProperties(o, {
    'b': { get: function() { return this.a + 1; } },
    'c': { set: function(x) { this.a = x / 2; } }
});

What I want is to define a default such that when we try o[unexpectedVar] it returns our default value.

Is this possible with the sort of set up above?

I'd like to set a default return value for an object.

If I have a set up like in the moz docs

var o = { a: 0 };

Object.defineProperties(o, {
    'b': { get: function() { return this.a + 1; } },
    'c': { set: function(x) { this.a = x / 2; } }
});

What I want is to define a default such that when we try o[unexpectedVar] it returns our default value.

Is this possible with the sort of set up above?

Share Improve this question asked Aug 4, 2017 at 9:55 AncientSwordRageAncientSwordRage 7,68421 gold badges100 silver badges193 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can make use of Proxy.

var o = {
  a: 5
};
var handler = {
    get: function(target, prop) {
      return target[prop]||'default value'
   }
}
var p = new Proxy(o,handler);
console.log(p.a) // this returns 5
console.log(p.unexpected) // this returns default value

So in this example, if you access defined property a it returns 5, but in case of an unexpected property it returns default value

I think you cannot set the default return of a object.

But you can set the default value of a object which in a function.

var o = { a: 0 };

test();

function test (o = { a: 1 }) {
   console.log(o.a);
}

Make use of Proxies available in ES6

var handler = {
  get: function(target, name) {
    return target.hasOwnProperty(name) ? target[name] : 42;
  }
};

var p = new Proxy({}, handler);

p.answerToTheUltimateQuestionOfLife; //=> 42

for more info check out https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy

I'd like to set a default return value for an object.

If I have a set up like in the moz docs

var o = { a: 0 };

Object.defineProperties(o, {
    'b': { get: function() { return this.a + 1; } },
    'c': { set: function(x) { this.a = x / 2; } }
});

What I want is to define a default such that when we try o[unexpectedVar] it returns our default value.

Is this possible with the sort of set up above?

I'd like to set a default return value for an object.

If I have a set up like in the moz docs

var o = { a: 0 };

Object.defineProperties(o, {
    'b': { get: function() { return this.a + 1; } },
    'c': { set: function(x) { this.a = x / 2; } }
});

What I want is to define a default such that when we try o[unexpectedVar] it returns our default value.

Is this possible with the sort of set up above?

Share Improve this question asked Aug 4, 2017 at 9:55 AncientSwordRageAncientSwordRage 7,68421 gold badges100 silver badges193 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can make use of Proxy.

var o = {
  a: 5
};
var handler = {
    get: function(target, prop) {
      return target[prop]||'default value'
   }
}
var p = new Proxy(o,handler);
console.log(p.a) // this returns 5
console.log(p.unexpected) // this returns default value

So in this example, if you access defined property a it returns 5, but in case of an unexpected property it returns default value

I think you cannot set the default return of a object.

But you can set the default value of a object which in a function.

var o = { a: 0 };

test();

function test (o = { a: 1 }) {
   console.log(o.a);
}

Make use of Proxies available in ES6

var handler = {
  get: function(target, name) {
    return target.hasOwnProperty(name) ? target[name] : 42;
  }
};

var p = new Proxy({}, handler);

p.answerToTheUltimateQuestionOfLife; //=> 42

for more info check out https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy

本文标签: Default getter value in JavascriptStack Overflow