最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

properties - Does JavaScript allow getters and setters? - Stack Overflow

programmeradmin2浏览0评论

this is my code:

<script type="text/javascript">
var Note=function(){}
Note.prototype = {
    get id()
    {
        if (!("_id" in this))
            this._id = 0;
        return this._id;
    },

    set id(x)
    {
        this._id = x;
    }
}

var a=new Note()
alert(a.id)
</script>

this style is like to python ,

this is my first time to see this code ,

and can you give me more example about 'get' and 'set' in javascript .

thanks

this is my code:

<script type="text/javascript">
var Note=function(){}
Note.prototype = {
    get id()
    {
        if (!("_id" in this))
            this._id = 0;
        return this._id;
    },

    set id(x)
    {
        this._id = x;
    }
}

var a=new Note()
alert(a.id)
</script>

this style is like to python ,

this is my first time to see this code ,

and can you give me more example about 'get' and 'set' in javascript .

thanks

Share Improve this question edited Aug 2, 2010 at 0:21 ChaosPandion 78.3k18 gold badges121 silver badges159 bronze badges asked Aug 2, 2010 at 0:02 zjm1126zjm1126 66.7k85 gold badges179 silver badges222 bronze badges 1
  • If you are going to support anything but the latest browsers, the real answer is no. At the bottom of Resig's post, he notes that only Firefox, Safari, and Opera support it. And ECMAScript 5 is not currently supported by any browser. It would be best to find another way to accomplish your goal. – Mark Eirich Commented Aug 2, 2010 at 2:09
Add a comment  | 

4 Answers 4

Reset to default 10

Yes it does. This feature was added in ECMAScript 5.

PropertyAssignment:
    PropertyName : AssignmentExpression 
    get PropertyName() { FunctionBody } 
    set PropertyName( PropertySetParameterList ) { FunctionBody } 

Here are a few things to remember when using this syntax.

  1. If your object literal has a value property it cannot have getter or setter and vice versa.
  2. Your object literal cannot have more than one getter or setter with the same name.

A better way to actually use this feature is through the Object.defineProperty function.

function Person(fName, lName) {
    var _name = fName + " " + lName;
    Object.defineProperty(this, "name", { 
        configurable: false, // Immutable properties!
        get: function() { return _name; } 
    });
}

This allows you to have nice clean objects with encapsulation.

var matt = new Person("Matt", "Richards");
console.log(matt.name);  // Prints "Matt Richards"

It can in certain engines, and it's in the spec for EcmaScript 5, so it should be more widely adopted in the future. The Compatibility Table doesn't direclty address this, but it will likely follow defineProperties, which provides an API for doing the same thing. As pointed out previously, John Resig has a nice article on the new object and property APIs.

Javascript does in fact support getters and setters now. John Resig has a good blog post about them here.

John's article does a good job at mentioning several different ways of defining getters/setters on Javascript objects, but doesn't do a good job at describing when each method is applicable. I believe that is much more effectively accomplished in a more recent blog post by Robert Nyman:

Getters and setters with JavaScript

(this article also introduces the ECMAScript Standard Object.defineProperty)

Yes it can. Here is a nice post about it from John Resig, the creator of jQuery:

JavaScript Getters and Setters

发布评论

评论列表(0)

  1. 暂无评论