Home  >  Article  >  Web Front-end  >  Learn more about JavaScript object accessors (Getter and Setter)

Learn more about JavaScript object accessors (Getter and Setter)

青灯夜游
青灯夜游forward
2019-11-29 14:39:242805browse

Learn more about JavaScript object accessors (Getter and Setter)

JavaScript Accessors (Getters and Setters), ECMAScript 5 (2009) introduced Getters and Setters. Getters and setters allow you to define object accessors (Computed Properties).

[Related course recommendations: JavaScript video tutorial]

JavaScript Getter (get keyword)

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>js</title>
<body>

<h2>JavaScript Getters和Setters</h2>

<p> Getters和setter允许您通过方法获取和设置属性。</p>

<p>此示例使用lang属性获取语言属性的值。</p>

<p id="demo"></p>

<script>
    // 新建一个对象。
    var person = {
        firstName: "John",
        lastName : "Doe",
        language : "en",
        get lang() {
            return this.language;
        }
    };
    // 使用getter显示来自对象的数据:
    document.getElementById("demo").innerHTML = person.lang;
</script>

</body>
</html>

JavaScript Setter (set keyword)

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Getters和Setters</title>
<body>

<h2> JavaScript Getters和Setters </h2>

<p> Getters和setter允许您通过方法获取和设置属性。</p>

<p>此示例使用lang属性设置语言属性的值。</p>

<p id="demo"></p>

<script>
    // Create an object:
    var person = {
        firstName: "John",
        lastName : "Doe",
        language : "NO",
        set lang(value) {
            this.language = value;
        }
    };
    // Set a property using set:
    person.lang = "en";
    // Display data from the object:
    document.getElementById("demo").innerHTML = person.language;
</script>

</body>
</html>

Use JavaScript function or Getter?

What is the difference between these two examples?

Example 1:

var person = {
  firstName: "John",
  lastName : "Doe",
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
// 使用方法显示对象的数据:
document.getElementById("demo").innerHTML = person.fullName();

Example 2:

var person = {
  firstName: "John",
  lastName : "Doe",
  get fullName() {
    return this.firstName + " " + this.lastName;
  }
};
// 使用getter显示来自对象的数据:
document.getElementById("demo").innerHTML = person.fullName;

Example 1 Access fullName as a function: person.fullName(). Example 2 accesses fullName as a property: person.fullName. The second example provides simpler syntax.

Data Quality

JavaScript ensures better data quality when using getters and setters. lang In this example, the attribute language is used to return the value of the attribute in uppercase:

// 创建一个对象:
var person = {
  firstName: "John",
  lastName : "Doe",
  language : "en",
  get lang() {
    return this.language.toUpperCase();
  }
};
// 使用getter显示来自对象的数据:
document.getElementById("demo").innerHTML = person.lang;

lang In this example, the attribute language is used to store the uppercase value in the language attribute:

var person = {
  firstName: "John",
  lastName : "Doe",
  language : "",
  set lang(lang) {
    this.language = lang.toUpperCase();
  }
};
// 使用setter设置对象属性:
person.lang = "en";
// 显示来自对象的数据:
document.getElementById("demo").innerHTML = person.language;

Why use Getter and Setter?

● It provides a simpler syntax

● It allows the same syntax for properties and methods

● It ensures better data quality

●It’s useful to do things behind the scenes

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>js</title>
<body>

<h2> JavaScript Getters和Setters </h2>

<p>完美的创建反对象:</p>

<p id="demo"></p>

<script>
    var obj = {
        counter : 0,
        get reset() {
            this.counter = 0;
        },
        get increment() {
            this.counter++;
        },
        get decrement() {
            this.counter--;
        },
        set add(value) {
            this.counter += value;
        },
        set subtract(value) {
            this.counter -= value;
        }
    };
    // Play with the counter:
    obj.reset;
    obj.add = 5;
    obj.subtract = 1;
    obj.increment;
    obj.decrement;
    // Display the counter:
    document.getElementById("demo").innerHTML = obj.counter;
</script>

</body>
</html>

Object.defineProperty()

The Object.defineProperty() method can also be used to add Getters and Setter:

// 定义对象
var obj = {counter : 0};
// 定义 setters
Object.defineProperty(obj, "reset", {
  get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
  get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
  get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
  set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
  set : function (value) {this.counter -= value;}
});
// Play with the counter:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;

Browser support

Getters and Setter are not supported in Internet Explorer 8 or earlier:

##9.0 Support SupportSupportSupport##This article comes from js tutorial
Internet Explorer Chrome FireFox Safari Opera

Learn more about JavaScript object accessors (Getter and Setter) Learn more about JavaScript object accessors (Getter and Setter) Learn more about JavaScript object accessors (Getter and Setter) Learn more about JavaScript object accessors (Getter and Setter)
column, welcome to learn!

The above is the detailed content of Learn more about JavaScript object accessors (Getter and Setter). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete