views:

141

answers:

4

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

+3  A: 

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"
ChaosPandion
A: 

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

JavaScript Getters and Setters

Felix Kling
+1  A: 

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.

Justin Love
+1 for more recent information and of course the awesome compatibility table.
ChaosPandion
+3  A: 

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)

Justin Niessner
Be aware that the post is a bit old (Jul 2007), it talks mostly about the non-standard `__defineSetter_`/`__defineGetter__`, which I would discourage its usage, and recommend (for the long term) to use the ES5 syntax, as @ChaosPandion explains.
CMS
I had to down-vote because you are directing people to outdated information. The article @Justin Love points to is a much better choice. http://ejohn.org/blog/ecmascript-5-objects-and-properties/
ChaosPandion
@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.
Justin Niessner
@Justin - I still don't think that's enough. If you include the more recent article as well I'll remove my down-vote.
ChaosPandion
@ChaosPandion - Done...but you might not like that post either.
Justin Niessner