Setters and getters in JavaScript are methods that are less exposed to in normal times. They are not standard methods themselves and are only supported in non-IE browsers (there may be other methods that can be done by the ie kernel? I don’t know the solution for the time being), but using them can do many things, such as:
1. Restrictions on data access:
a.value is a getter method call to the value variable. If an exception is thrown in the getter method implementation, access to the value variable can be blocked.
2. Listen to dom variables:
window.name is a very useful dom attribute across domains (known for its details, see Baidu for details). If you override the setter implementation of window.name, you can realize asynchronous memory communication across pages.
3. Use your imagination and can do a lot of things
All the following are transferred:
First, let's take a quick look at what Getters and Setters are and why they are useful. Then, let's see which platforms now support Gettets and Setters.
Getters and Setters
Getters and Setters enable you to quickly get or set up the data of an object. Generally speaking, an object has two methods, which are used to obtain and set a certain 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 to be directly accessed by the outside world. 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 it like this:
var field = new Field("test");field.value// => undefinedfield.setValue("test2")field.getValue()// => "test2"Let's simulate the "hidden value attribute" in the example above, and our code looks like this:
function Field(val){var value = val;this.__defineGetter__("value", function(){return value;});this.__defineSetter__("value", function(val){value = val;});}However, you don't like to write this way, and tend to define getters and setters in the object's prototype (it doesn't matter where the private variable is written), 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 after using it for a while, it's easy to accept it.
Next is another example, which allows the outside world to get an array of usernames, but cannot get the original, hidden user objects.
function Site(users){this.__defineGetter__("users", function(){// JS 1.6 Array map()return users.map(function(user){return user.name;});};}Remember the following points:
Within an object, each variable can only have one getter or setter. (So value can have a getter and a setter, but value never has two getters)
The only way to delete a getter or setter is: delete object[name]. delete can remove some common properties, getters and setters.
If you use __defineGetter__ or __defineSetter__, it overrides the getter or setter of the same name defined previously, or even a property (property).
platform
Supported browsers are:
Firefox
Safari 3+
Opera 9.5
The above is the full description of the setter and getter methods introduced in JavaScript that the editor has introduced to you. I hope it will be helpful to you. If you want to know more, please pay attention to Wulin.com.