1.在对象中定义get,set访问器属性

<script>
        var test = {
            _name:"pmx",
            _age:18,
            _born:1990,
            get name(){
                return "name is "+this._name;
            },
            set name(value){
                this._name = value;
            },
            get born(){
                return this._born;
            },
            set born(value){
                this._born = value;
            },
            get age(){
                if(this._age > 100){
                    return new Date().getFullYear() - this.born;
                }else{
                    return this._age;
                }
            },
            set age(value){
                this._age = value;
            }
        }
        console.log(test.age);
        test.age = 2016;
        console.log(test.age);
</script>

2.使用defineProperty给对象添加访问器

<script>
        var test = {
            _name:"pmx",
            _age:18,
            _born:1990

        }
        Object.defineProperty(test,"name",{
            get:function(){
                return "name is "+this._name;
            },
            set:function(value){
                this._name = value;
            }
        });
        Object.defineProperties(test,{
            age:{
                get:function(){
                    if(this._age > 100){
                            return new Date().getFullYear() - this.born;
                        }else{
                        return this._age;
                    }
                },
                set:function(value){
                    this._age = value;
                }
            },
            born:{
                get:function(){
                    return this._born;
                },
                set:function(value){
                    this._born = value;
                }
            }
        });
         console.log(test.age); //18
         test.age = 2016;
        console.log(test.age); //26
</script>

3.在类中添加访问器

<script>
        function test(name,age){
            this._name = name;
            this._age = age;

            Object.defineProperty(this,"name",{
                get:function(){
                    return "name is "+this._name;
                }
            });
        }
        var tt = new test('pmx',26);
        console.log(tt.name);
</script>

最新文章

  1. for循环或Repeat里面对某个字段进行复杂处理的解决方案
  2. 广义Euler常数
  3. Java串口通信详解
  4. linux shell脚本守护进程监控svn服务
  5. dubbo-admin管理平台搭建
  6. oracle创建job方法
  7. [转] 关于c++的头文件依赖
  8. 基于bootstrap的datatable控件
  9. leetcode70
  10. 阿里开源的热补丁框架AndFix使用教程
  11. go语言时间比较
  12. SQL SERVER获取信息的方法
  13. Kali Linux常用服务配置教程获取IP地址
  14. Netty ByteBuf 和 String 转换
  15. insert select带来的问题
  16. 2017&quot;百度之星&quot;程序设计大赛 - 初赛(A) 01,05,06
  17. 客户端代码压缩成zip和服务器开启gzip
  18. 802.1X技术介绍
  19. Quartz 任务调度框架之Hello World
  20. IO多路复用(select)

热门文章

  1. ural 1250. Sea Burial
  2. BZOJ 1053 &amp; 反素数
  3. 关于struts2中表单提交时,中文乱码问题的解决
  4. Codeforces Round #228 (Div. 2) A. Fox and Number Game
  5. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试
  6. xargs
  7. AJAX回调(调用后台方法返回数据)
  8. respond.min.js IE失效问题
  9. 初始化char指针--赋值和strcpy() 本质区别【转】
  10. VC++6.0编译器标记的那些内存值