English 中文(简体)
Java文中的“无天”洗衣机和板
原标题:"Nested" getter and setters in JavaScript

我想要利用联合材料集束器和接收器实现类似于这一组的合成:

globe.camera.position = Position.create();

这被认为相当于这一表述:

globe.getCamera().setPosition(Position.create());

我在创建“第一层”的采集器/设计器方面没有任何问题。

function Camera() {

    var x,y,z;

this.__defineGetter__("camera", function() {
        alert("This is the camera getter");
});

    this.__defineSetter__("camera", function(position) {            
        alert("This is the camera setter");
});

}

...

globe=new Camera();
globe.camera=...
c=globe.camera;

...

但是,在如何界定立场时,并不十分确定。 我正试图这样做,但赢得了以下工作:

function Position() {

    this.__defineGetter__("position", function() {
        alert("This is the position getter");
});

}

globe.camera=new Position();
pos=globe.camera.position;

耳机的警示显示。 这方面的任何cl? 是否甚至有可能实现这种行为? 我在谷歌上搜索了很多东西,但本特能够打上正确的搜索条件,而登机/打字机的例子往往非常简单。 提前感谢。

最佳回答

<代码>defineGetter__,朋友们不是标准。

var Camera = Object.create({}, {
  position: {
    get: function () {
      return Position.create();
    },
    set: function (p) {
      this._position = p;
    }
  }
});

Object.defineProperty(globe, "camera", {
  get: function () {
    return Object.create(Camera);
  }
});

页: 1

However, why are you using getters and setters? They are evil. You should avoid them really unless your doing something clever.

问题回答

暂无回答




相关问题
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.

热门标签