English 中文(简体)
In JScript, is it possible to implement getters and setters that look like object properties from the outside?
原标题:

While trying to port and generally playing around with some non-browser code, I came across getters and setters that looked like normal object properties. Something like this:

js> var o = {
    a: 4,
    get b(){
        return this.a + 3;
    },
    set b(val){
        this.a = val - 3;
    }
};
js> o.a
4
js> o.b
7
js> o.b=10
10
js> o.a
7

This seems to work in recent versions of Rhino and Spidermonkey, but is it possible to implement or simulate the behavior (the defining syntax is less important to me) in JScript (Windows Script Host)?

最佳回答

The answer is No. Setters and getters are just properties that act like functions, but there is no way to emulate the syntax correctly. I had a half-way concept of emulating getters and setters on HTML elements in <=IE7 using behaviors, but even that turned out to be more difficult than I first imagined it would. Even IE8 only supports getters/setters on DOM objects and not JScript objects, so I think it s something the JScript team need to include, if they ever do.

If only someone had thought to include setters and getters in the original JScript/ECMAScript implementations.

问题回答

According to this article (by John Resig, creator of jQuery), Javascript getters and setters are supported in JScript.NET 8.

This si a complete list of browsers and their support for getters and setters. http://robertnyman.com/javascript/#javascript-getters-setters-object-defineproperty-compatibility





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

热门标签