English 中文(简体)
使用对象. defineProperty 给予两个属性
原标题:Using Object.defineProperties gives two properties

我正在尝试创建一个对象。 但我不明白为什么我的属性获取器设置器不能简单地调用 this.bar 。 因此,我的 foo 对象似乎有两个属性。

这是正确的,还是我:

  1. using defineProperties wrong
  2. missing the point

创建 foo , 带有 bar 属性

var foo = function ()
{   
    Object.defineProperties(this, {
        bar : {
            get : function () {return this.barVal},
            set : function(value) { this.barVal = value},
            enumerable: true,
            configurable: true
        }
    })
};

var o = new foo();
o.bar = "Hello";
console.log(JSON.stringify(o));

//output {"bar":"Hello","barVal":"Hello"} 
最佳回答

JSON. stritizing 激活获取者来解决财产问题。 您的设置者设置了第二个属性, 所以最后您可以同时看到这两个属性 。 您需要的是一种方式来存储 foo.bar 的“ 内部” 值, 而不是 foo.bar 本身 。

function Foo(){
  var secret = {};
  Object.defineProperties(this, {
    bar: {
      get: function( ){ return secret.bar },
      set: function(v){ secret.bar = v },
      enumerable: true,
      configurable: true
    }
  });
}


var foo = new Foo;
foo.bar =  Hello ;
console.log(JSON.stringify(foo));
问题回答

您正在创建两个属性, 一个名为“ bar”, 另一个名为“ barVal ” 。 “ bar” 是由定义Properies call 创建的, 而 “ barVal” 是由此设置函数中的此.barVal 任务创建的。 它们都对可量化属性具有真实值( 您为 varVal 明确设定了该属性, 指定为 barVal ), 因此它们都由 JSON. strigize 列出 。

如果您的意图是将Barval作为私人价值对待,而该私人价值在 JSON 中没有出现,或者作为前例列出,您可以明确地将其数字属性设定为虚假:

var foo = function ()
{   
    Object.defineProperties(this, {
        bar : {
            get : function () {return this.barVal},
            set : function(value) { this.barVal = value},
            enumerable: true,
            configurable: true
        },
        barVal : {
            value: undefined,
            enumerable: false, writable: true, configurable: true
        }
    })
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签