在 JavaScript 中定义哈希成员的 setter

发布于 2024-09-30 00:01:42 字数 306 浏览 9 评论 0原文

我有这样定义的哈希:

var props = {
    id: null,
    title: null,
    status: null
};

我想为状态字段(并且仅针对它)定义设置器,如下所示:

props.__defineSetter__("status", function(val){
    //Checking correctness of val...
    status = val;
});

但它不起作用:( 那么,正确的做法是什么?

I have hash defined like this:

var props = {
    id: null,
    title: null,
    status: null
};

I'd like to define setter for status field (and only for it) doing it as following:

props.__defineSetter__("status", function(val){
    //Checking correctness of val...
    status = val;
});

But it doesn't work :(
So, what's the right way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞旧 2024-10-07 00:01:42

很简单,您需要使用

this.status = val;

否则您只是将一个不相关的全局变量status设置为等于val

正如已经指出的,setter/getter 并未在 IE 中实现。

另外,我不确定拥有一个与其设置的属性同名的设置器是否明智。不确定这是否会导致冲突,但这似乎是一个坏主意,是吗?理想情况下,要设置的变量应该隐藏在闭包中

var props = {
  id: null,
  title: null
};

(function() {

  var status;
  props.__defineSetter__("status", function(val){
    //Checking correctness of val...
    status = val;
  });

  props.__defineGetter__('status', function() { return status; });

}());

,这样,status 就可以完全防止直接访问,这就是使用 setter 和 getter 的要点。

Simple, you need to use

this.status = val;

Otherwise you are just setting an unrelated global variable status equal to val.

And as already noted, setters/getters are not implemented in IE.

Also, I'm not sure about how wise it is to have a setter that is the same name as the property it sets. Not sure if this will result in a conflict, but it does seem like a bad idea yes? Ideally the variable that would be set should be hidden in a closure

var props = {
  id: null,
  title: null
};

(function() {

  var status;
  props.__defineSetter__("status", function(val){
    //Checking correctness of val...
    status = val;
  });

  props.__defineGetter__('status', function() { return status; });

}());

This way, status is fully protected from direct access, which is the point of using setters and getters.

今天小雨转甜 2024-10-07 00:01:42

首先是MooGoo所指出的。而且,您不能使用与对象中现有变量相同的名称将属性设置器分配给对象。

所以你的代码必须是围绕这个的:

var props = {
    id: null,
    title: null,
    hStatus: null,
};

props.__defineSetter__("status", function(v){
    this.hStatus = v;
});

props.__defineGetter__("status", function(){
    return this.hStatus;
});

[编辑]
是的,MooGoo 编辑他的答案的速度比我写这个 8(.

The first thing is what MooGoo has pointed out. But also, you can not assign a property setter to an object using the same name as an existing variable in the object.

So your code would have to be something arround this:

var props = {
    id: null,
    title: null,
    hStatus: null,
};

props.__defineSetter__("status", function(v){
    this.hStatus = v;
});

props.__defineGetter__("status", function(){
    return this.hStatus;
});

[edit]
Yeah, MooGoo eddited his answer faster then time time I took to write this 8(.

沧笙踏歌 2024-10-07 00:01:42

这应该有效:

props.__setStatus__ = function(val) {
    // Check correctness of val
    this.status = val;
}

用法:

props.__setStatus__('Alive');

This should work:

props.__setStatus__ = function(val) {
    // Check correctness of val
    this.status = val;
}

Usage:

props.__setStatus__('Alive');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文