JavaScript in the setter, getter is the usual method of less contact, which itself is not a standard method, only in non-IE browser support (ie kernel may have other ways to do it?) For the time being, but there are many things you can do with it, such as:
1. Access Restrictions on data:
A.value is a getter method call to a value variable that prevents access to the value variable if an exception is thrown in the Getter method implementation
2. Monitor the DOM variables:
Window.name is a cross domain very useful DOM attribute (well-known, see Baidu), if the overlay window.name setter implementation can be implemented across the page of memory asynchronous communication
3, to play their own imagination, can do things a lot of drops
All of the following:
First, let's take a quick look at what getters and setters are and why they are useful. Then let's take a look at what platforms now support gettets and setters.
Getters and Setters
Getters and setters enable you to quickly get or set an object's data. In general, an object has two methods for getting and setting a value, such as:
{
getvalue:function () {return
this._value;
},
Setvalue:function (val) {
this._value = val;
}
}
One obvious benefit of writing JavaScript in this way is that you can use it to hide attributes that you don't want the outside world to access directly. The final code looks like this (save the value of the newly created filed object with a closure):
Function Field (val) {
var value = val;
This.getvalue = function () {return
value;
};
This.setvalue = function (val) {
value = val;}
;
}
So we can use this:
var field = new Field ("Test");
Field.value
//=> undefined
field.setvalue ("test2")
Field.getvalue ()
Let's simulate the "hidden value attribute" in the example above, and our code is like this:
Function Field (val) {
var value = val;
THIS.__DEFINEGETTER__ ("Value", function () {return
value;
});
THIS.__DEFINESETTER__ ("Value", function (val) {
value = val;}
);
But, you don't like to write like this, and you tend to define getters and setters in the prototype of the object (where the private variable is written is not important), we can use another syntax.
Function Field (val) {
this.value = val;
}
Field.prototype = {Get
value () {return
this._value;
},
set Value (val) {
this._value = val;
}
};
This syntax looks incredible, but it's easy to accept it after a while.
Next is another example that allows the outside world to get a username array, but cannot get the original, hidden user object.
function Site (users) {
this.__definegetter__ ("Users", function () {
//JS 1.6 Array Map () return
Users.map ( function (user) {return
user.name;}
);
}
Keep the following points in mind:
Within an object, each variable can have only one getter or setter. (so value can have a getter and a setter, but value is never two getters)
The only way to remove a getter or setter is to delete object[name]. Delete can remove some common properties, getters and setters.
If you use __definegetter__ or __definesetter__, it overrides the getter or setter, or even the property, that was previously defined with the same name.
Platform
Supported browsers are:
Firefox
Safari 3+
Opera 9.5
The above is a small set to introduce the JavaScript setter and Getter method introduction of all the narrative, hope to help everyone, if you want to learn more, please pay attention to cloud habitat community.