zoukankan      html  css  js  c++  java
  • get and set

     1 (function () {
     2 
     3     function Student(name, age, gender) {
     4         this._name = name;
     5         this._age = age;
     6         this._gender = gender;
     7     }
     8 
     9     Object.defineProperty(Student.prototype, "name", {
    10         get: function () {
    11             return this._name;
    12         },
    13         set: function (value) {
    14             this._name = value;
    15         }
    16     });
    17     Object.defineProperty(Student.prototype, "age", {
    18         get: function () {
    19             return this._age;
    20         },
    21         set: function (value) {
    22             this._age = value;
    23         }
    24     });
    25     Object.defineProperty(Student.prototype, "gender", {
    26         get: function () {
    27             return this._gender;
    28         }
    29     });
    30 
    31 
    32     var stu = new Student("张三", 12, "男");
    33     console.log(stu.name);
    34     stu.age=13;
    35     console.log(stu.age);
    36 })();

    在只提一下Object.defineProperty方法。

    三个参数:目标对象,方法名,功能(get和set)

    注意set只能接受一个数值。

    今天偶然想到,set的一个巧用。用它可以代替形参,有时候。具体是不去写形参,而是在用它的时候去拿set去赋值。

    代码进化:

     1 (function () {
     2 
     3     function Student(name, age, gender) {
     4         this._name = name;
     5         this._age = age;
     6         this._gender = gender;
     7     }
     8 
     9     Object.defineProperties(Student.prototype, {
    10         name: {
    11             set: function (value) {
    12                 this._name = value;
    13             },
    14             get: function () {
    15                 return this._name;
    16             }
    17         },
    18         age: {
    19             set: function (value) {
    20                 this._age = value;
    21             },
    22             get: function () {
    23                 return this._age;
    24             }
    25         },
    26         gender: {
    27             get: function () {
    28                 return this._gender;
    29             }
    30         }
    31     });
    32 
    33     function main() {
    34         var stu1 = new Student("Tom", 12, "boy");
    35         console.log(stu1.gender);
    36     }
    37 
    38     main();
    39 })();

    这里用Object.defineProperties批量设置属性。注意格式就行。

    代码装甲进化:

     1 (function () {
     2 
     3     function Student(name, age, gender) {
     4         return {
     5             _name: name,
     6             _age: age,
     7             _gender: gender,
     8             get name() {
     9                 return this._name;
    10             },
    11             set name(value) {
    12                 this._name=value;
    13             },
    14             get age(){
    15                 return this._age;
    16             },
    17             set age(value){
    18                 this._age=value;
    19             },
    20             get gender(){
    21                 this._gender=gender;
    22             }
    23         };
    24 
    25     }
    26 
    27     var stu1=new Student("Tom",12,"female");
    28     console.log(stu1.name);
    29 })();

    这样可以把get和set写在内部。同样注意格式。

  • 相关阅读:
    lua编程之协程介绍
    lua编程之元表与元方法
    设计模式系列之单例模式
    设计模式系列之生成器模式
    设计模式系列之抽象工厂模式
    设计模式系列之原型模式
    设计模式系列之工厂模式
    stl源码分析之hash table
    2018/2019款 MacBookPro 接口失灵的原因及解决方案
    test
  • 原文地址:https://www.cnblogs.com/chenluomenggongzi/p/5811530.html
Copyright © 2011-2022 走看看