zoukankan      html  css  js  c++  java
  • 访问器属性:setter()函数和getter()函数

    1.干嘛用的?

      getter()函数:返回有效的值

      setter()函数:调用它并传入数据,这个函数决定如何处理数据

    2.具备哪些属性?如何定义?

      configurable(默认为true),enumerable(默认为true),get(默认为undefined),set(默认为undefined) 四个属性特征

    3.具体应用?

    var book={_year:2017,edition:1};
    
    Object.defineProperty(book,"year",{
        get:function(){return this._year},
        set:function(newValue){
                if(newValue>2004){
                    this._year=newValue;
                    this.edition+=newValue-2004
                }
        }
    });
    
    book.year=2005;
    alert(book.edition);//2
    alert(book._year); //2005
  • 相关阅读:
    bzoj2959
    学习笔记::lct
    bzoj3203
    bzoj1319
    bzoj3625
    bzoj3992
    bzoj1565
    bzoj3513
    平常练习动归(1.胖男孩)———最长公共子序列
    2016 noip 复赛 day2
  • 原文地址:https://www.cnblogs.com/n2meetu/p/6739491.html
Copyright © 2011-2022 走看看