javascript 类中的 JS 吸气剂和吸气剂?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21564885/
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
JS getters and setters inside a class?
提问by Kuba Orlik
I'd like to create a class in JS that uses native getters and setters. I know I can create getters/setters for objects, like so:
我想在 JS 中创建一个使用本机 getter 和 setter 的类。我知道我可以为对象创建 getter/setter,如下所示:
var obj = {
get value(){
return this._value;
},
set value(val){
this._value = val;
}
}
I also know that I can use this.__defineGetter__
inside a class/function, but MDN says that using __defineGetter__()
etc is discauraged.
我也知道我可以this.__defineGetter__
在类/函数中使用,但 MDN 表示不__defineGetter__()
鼓励使用etc。
Is there any better way to add getters and setters to js class than:
有没有比以下更好的方法将 getter 和 setter 添加到 js 类:
function class(){
};
class.prototype = {
get value(){
//....
}
?
?
回答by Matthew Amato
The cleanest way to define properties on a class is via Object.defineProperties. This allows you to define all of your properties in a single, easily readable block. Here's an example:
在类上定义属性的最简洁的方法是通过Object.defineProperties。这允许您在一个易于阅读的块中定义所有属性。下面是一个例子:
var MyClass = function() {
this._a = undefined;
this._b = undefined;
};
Object.defineProperties(MyClass.prototype, {
//Create a read-only property
a : {
get : function() {
return this._a;
}
},
//Create a simple read-write property
b : {
get : function() {
return this._b;
},
set : function(value) {
this._b = value;
}
}
});
There are a plethora of other options when defining properties, so be sure to check out the link I posted for more information. It's also important to keep in mind that even the most basic getter/setter property is only as fast as a method call in current browsers, so they can become a bottleneck in performance-intensive situation.
定义属性时还有很多其他选项,因此请务必查看我发布的链接以获取更多信息。同样重要的是要记住,即使是最基本的 getter/setter 属性也只与当前浏览器中的方法调用一样快,因此它们可能成为性能密集型情况的瓶颈。
回答by user1160006
2019: Hooray for ES6!
2019 年:ES6 万岁!
class Person {
get name() {
return this._name + '!!!'
}
set name(newValue) {
this._name = newValue
}
constructor(name) {
this._name = name
}
}
const me = new Person('Zach')
console.log(me.name) // Zach!!!
me.name = 'Jacob'
console.log(me.name) // Jacob!!!
// Of course, _name is not actually private.
console.log(me._name) // Jacob
回答by user2845946
How about this implementation:
这个实现怎么样:
function makeObject(obj, name) {
// The property
var value;
// The setter
obj["get" + name] = function() {return value;};
// The getter
obj["set" + name] = function(v) {
value = v;
};
}
To experiment:
去体验:
var obj = {};
makeObject(obj, "Name");
obj.setName("Lolo");
print(obj.getName());
Of course, you can test name
for validity before storing it in value
. The test can be supplied as an additional argument to the makeObject
function.
当然,您可以name
在将其存储在value
. 测试可以作为makeObject
函数的附加参数提供。