9

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

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. Commented Aug 2, 2010 at 2:09

4 Answers 4

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"
Sign up to request clarification or add additional context in comments.

Comments

6

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.

2 Comments

+1 for more recent information and of course the awesome compatibility table.
This table shows that it is supported on object literals, but not object prototypes (in IE): robertnyman.com/javascript/…
3

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)

4 Comments

I had to down-vote because you are directing people to outdated information. The article @Justin Love points to is a much better choice. ejohn.org/blog/ecmascript-5-objects-and-properties
@CMS @ChaosPandion - The article I provided talks about several different methods of implementing getters/setters in JavaScript. One of the mentioned methods is the current supported standard. The OP will still benefit from learning about the others and what the differences are.
I still don't think that's enough. If you include the more recent article as well I'll remove my down-vote.
@ChaosPandion - Done...but you might not like that post either.
0

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

JavaScript Getters and Setters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.