Introduction to setter and getter methods in JavaScript

Source: Internet
Author: User
This article mainly introduces related information about the setter and getter methods in JavaScript, which is very good and has reference value, for more information, see the setter and getter methods in javascript. They are not standard methods, it is only supported in non-ie browsers (maybe there are other ways to do it in ie kernel? But it can be used to do many things, such:

1. Data Access restrictions:

A. value is a call to the getter method of 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 dom attribute that is very useful for Cross-Domain Communication (see Baidu for details). If the setter implementation that covers window. name can implement asynchronous inter-page memory communication.

3. Make full use of your imagination and do a lot of things

The conversion is as follows:
First, let's get to know what Getters and Setters are and why they are useful. Next, let's see which platforms currently support Gettets and Setters.

Getters and Setters

Getters and Setters allow you to quickly obtain or set the data of an object. Generally, an object has two methods for obtaining and setting a value, for example:

{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 do not want external access to directly. The final code looks like the following (use a closure to save the value of the newly created Filed object ):

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 above example. Our code is like this:

function Field(val){var value = val;this.__defineGetter__("value", function(){return value;});this.__defineSetter__("value", function(val){value = val;});}

However, you do not like writing like this, but tend to define getters and setters in the prototype of the object (private variables are 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 is easy to accept it after a while.

Next is another example. It allows the external world to obtain a username array, but cannot obtain 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;});};}

Remember the following:

In an object, each variable can only have one getter or setter. (Therefore, the value can have one getter and one setter, but the value never has two getters)
The only method to delete a getter or setter is delete object [name]. Delete can delete some common attributes, getters and setters.
If _ defineGetter _ or _ defineSetter __is used, it will overwrite the getter or setter with the same name as previously defined, or even the property ).

Platform

Supported browsers include:

Firefox
Safari 3 +
Opera 9.5

The above section describes all the descriptions of setter and getter methods in JavaScript. I hope it will be helpful to you. If you want to learn more, please stay tuned to the PHP Chinese website.

For more information about the setter and getter methods in JavaScript, refer to PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.