Javascript 进阶 封装


 <script type="text/javascript">

        /**

         * 使用约定优先的原则,把所有的私有变量都使用_开头

         */

        var Person = function (no, name, age)

        {

            this.setNo(no);

            this.setName(name);

            this.setAge(age);

        }

        Person.prototype = {

            constructor: Person,

            checkNo: function (no)

            {

                if (!no.constructor == "string" || no.length != 4)

                    throw new Error("学号必须为4位");

            },

            setNo: function (no)

            {

                this.checkNo(no);

                this._no = no;

            }, getNo: function ()

            {

                return this._no;

            }, setName: function (name)

            {

                this._name = name;

            }, getName: function ()

            {

                return this._name;

            }, setAge: function (age)

            {

                this._age = age;

            }, getAge: function ()

            {

                return this._age;

            }, toString: function ()

            {

                return "no = " + this._no + " , name = " + this._name + " , age = " + this._age;

            }

        };

        var p1 = new Person("0001", "鸿洋", "22");

        console.log(p1.toString());        //no = 0001 , name = 鸿洋 , age = 22

        p1.setNo("0003");

        console.log(p1.toString());      //no = 0003 , name = 鸿洋 , age = 22

        p1.no = "0004";

        p1._no = "0004";

        console.log(p1.toString());    //no = 0004 , name = 鸿洋 , age = 22



    </script>

看完代码,是不是有种被坑的感觉,仅仅把所有的变量以_开头,其实还是可以直接访问的,这能叫封装么,当然了,说了是约定优先嘛,这种方式还是不错的,最起码成员变量的getter,setter方法都是prototype中,并非存在对象中,总体来说还是个不错的选择。如果你觉得,这不行,必须严格实现封装,那么看第二种方式。

2、严格实现封装


<script type="text/javascript">

        /**

         *  使用这种方式虽然可以严格实现封装,但是带来的问题是get和set方法都不能存储在prototype中,都是存储在对象中的

         * 这样无形中就增加了开销

         */

        var Person = function (no, name, age)

        {

            var _no , _name, _age ;

            var checkNo = function (no)

            {

                if (!no.constructor == "string" || no.length != 4)

                    throw new Error("学号必须为4位");

            };

            this.setNo = function (no)

            {

                checkNo(no);

                _no = no;

            };

            this.getNo = function ()

            {

                return _no;

            }

            this.setName = function (name)

            {

               _name = name;

            }



            this.getName = function ()

            {

                return _name;

            }



            this.setAge = function (age)

            {

                _age = age;

            }

            this.

                    getAge = function ()

            {

                return _age;

            }



            this.setNo(no);

            this.setName(name);

            this.setAge(age);

        }

        Person.prototype = {

            constructor: Person,

            toString: function ()

            {

                return "no = " + this.getNo() + " , name = " + this.getName() + " , age = " + this.getAge();

            }

        }

        ;

        var p1 = new Person("0001", "鸿洋", "22");

        console.log(p1.toString());        //no = 0001 , name = 鸿洋 , age = 22

        p1.setNo("0003");

        console.log(p1.toString());      //no = 0003 , name = 鸿洋 , age = 22

        p1.no = "0004";

        console.log(p1.toString());    //no = 0003 , name = 鸿洋 , age = 22



    </script>

看上面的代码,去掉了this.属性名,严格的实现了封装,只能通过getter,setter访问成员变量了,但是存在一个问题,所有的方法都存在对象中,增加了内存的开销。

3、以闭包的方式封装


<script type="text/javascript">

        /**

         *  使用这种方式虽然可以严格实现封装,但是带来的问题是get和set方法都不能存储在prototype中,都是存储在对象中的

         * 这样无形中就增加了开销

         */

        var Person = (function ()

        {

            var checkNo = function (no)

            {

                if (!no.constructor == "string" || no.length != 4)

                    throw new Error("学号必须为4位");

            };

            //共享变量

            var times = 0;



            return function (no, name, age)

            {

                console.log(times++);    // 0 ,1 , 2

                var no , name , age;

                this.setNo = function (no)

                {

                    checkNo(no);

                    this._no = no;

                };

                this.getNo = function ()

                {

                    return this._no;

                }

                this.setName = function (name)

                {

                    this._name = name;

                }



                this.getName = function ()

                {

                    return this._name;

                }



                this.setAge = function (age)

                {

                    this._age = age;

                }

                this.

#### 算法刷题

大厂面试还是很注重算法题的,尤其是字节跳动,算法是问的比较多的,关于算法,推荐《LeetCode》和《算法的乐趣》,这两本我也有电子版,字节跳动、阿里、美团等大厂面试题(含答案+解析)、学习笔记、Xmind思维导图均可以分享给大家学习。



![](https://img-blog.csdnimg.cn/img_convert/c582a01373152bb4cd38bc6ad5cc8027.png)



**写在最后**

**最后,对所以做Java的朋友提几点建议,也是我的个人心得:**

1.  疯狂编程

2.  学习效果可视化

3.  写博客
4.  阅读优秀代码
5.  心态调整


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值