javascript 什么是 __defineGetter__() 和 __defineSetter__() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6825191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are __defineGetter__() and __defineSetter__() functions?
提问by Sergey Metlov
What are __defineGetter__()
and __defineSetter__()
functions in prototype of every Object
?
每个的原型中有什么__defineGetter__()
和__defineSetter__()
功能Object
?
采纳答案by nrabinowitz
See the MDN docs herefor a description and example code:
A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties.
getter 是一种获取特定属性值的方法。setter 是一种设置特定属性值的方法。您可以在任何支持添加新属性的预定义核心对象或用户定义对象上定义 getter 和 setter。
As noted in the docs (and by @ cwallenpoole), __define[GS]etter__()
functions are now deprecated. There's a lot more detail in this article. I believe the defineProperty()function is now the preferred syntax.
正如文档中(以及@cwallenpoole)所指出的,__define[GS]etter__()
函数现在已被弃用。这篇文章中有更多细节。我相信defineProperty()函数现在是首选语法。
回答by Guy
To answer your question __defineGetter__()
and __defineSetter__()
are the old/original way to create a getter and a setter for an object's property. They allow you use an object's property as a name/value pair while behind the scenes these name/value pairs are supported by functions.
回答您的问题__defineGetter__()
,__defineSetter__()
是为对象的属性创建 getter 和 setter 的旧/原始方法。它们允许您将对象的属性用作名称/值对,而在幕后,函数支持这些名称/值对。
For example, let's say you wanted to reference some random numbers in fixed ranges. You could express these as words with the maximum of the range and it would look like a property.
例如,假设您想在固定范围内引用一些随机数。您可以将这些表示为具有最大范围的单词,它看起来像一个属性。
var random = {};
random.__defineGetter__('ten', function() {
return Math.floor(Math.random()*10); });
random.__defineGetter__('hundred', function() {
return Math.floor(Math.random()*100); });
Note that the while the above example answers the question you should not use this solution. Instead you should use the modern form of getters and setters since ES5:
请注意,虽然上面的示例回答了您不应使用此解决方案的问题。相反,您应该使用 ES5 以来现代形式的 getter 和 setter:
var random = {
get ten() { return Math.floor(Math.random()*10); },
get hundred() { return Math.floor(Math.random()*100); }
};
Either of the above constructs would allow you to get a random number like this:
上述任何一种构造都可以让您获得这样的随机数:
var myrand = random.ten;
// returns a result in the range 0 to 9
回答by lil
.__defineGetter__
it means when you refer to object.[param1] a function executed.
.__defineSetter__
it means when you set object.[param1] a function executed.
for example, like this:
.__defineGetter__
这意味着当您引用 object.[param1] 执行的函数时。
.__defineSetter__
这意味着当您设置 object.[param1] 时执行了一个函数。例如,像这样:
const person = {
firstName: 'john',
lastName: 'doe',
};
person.__defineGetter__('fullName', () => `${person.firstName} ${person.lastName}`);
person.__defineSetter__('fullName', v => {
person.firstName = v.split(' ')[0];
person.lastName = v.split(' ')[1];
});
or if you want cls
to clear the console,
或者如果你想cls
清除控制台,
this.__defineGetter__('cls', console.clear);