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 badges3 Answers
Reset to default 3You 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 badges3 Answers
Reset to default 3You 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
版权声明:本文标题:Default getter value in Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657845a2161704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论