码迷,mamicode.com
首页 > 编程语言 > 详细

javascript实例教程之封装的用法

时间:2016-05-24 18:52:10      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

今天讲讲javascript设计模式中的包装明星——封装,我们会把现实中的一些事物抽象成一个Class并且把事物的属性(名词)作为ClassProperty把事物的动作(动词)作为Classmethods。在面向对象的语言中(C#等)都会有一些关键字来修饰类或者属性(Privatepublicprotect),这些关键词描述了访问的权限,不多做解释。

 我们来看看Javascript的易变的特性(我们还用上一次的例子):

var Man = function (name, age) {

             this.Name = name;

             this.Age = age;

       }

      var Person = new Interface("Person", ["GetName", "GetAge"]);

            Man.prototype = { GetName: function () { return this.Name; },

            GetAge: function () { return this.Age; }

        }     

      var Alan = new Man("Alan", 25);

      alert(Alan.GetAge());

    Alan.DisplayAll = function () { return "Name: "+this.GetName() + "; Age: " + this.GetAge() }

   alert(Alan.DisplayAll());

我先创建了一个ClassJavascript的匿名方法)拥有2个公共的(public)的字段(本篇blog会详细讲解,继续往下看)和2public的方法,我们创建了一个Instance--Alan,但是我可以为这个Instance动态的添加一个DisplayAll的方法,我想任何面向对象的语言是做不到这一点的,Javascript的灵活体现之一。

我们现在假设一个场景,如果有很多的程序员要用这段代码,由于Javascript的易变性,程序员就可以在实例化后改变Name的值,那初始化的动作就没有意义了:

 var Alan = new Man("Alan", 25);

       Alan.Name = "Alice"; //悲剧了,我alert的时候变成Alice

       alert(Alan.GetName());

所以我们不能让外部的人去任意的修改这个字段,在JavaC#中我们只需要个这个字段改为Private,就万事OK了,但是Javascript没有这个关键词,那我们需要这么做呢,这就是这篇blog存在的意义尴尬

我们可以想下在C#除了设置Private之外我们还可以怎么做?我们可以设置SetterGetter方法。

我们来修改下上面的代码:我们称方法一:

 var Person = new Interface("Person", ["SetName", "SetAge", "GetName", "GetAge"]);

        var Man = function (name, age) {

            this.SetAge(age);

            this.SetName(name);

        }

        Man.prototype = {

            SetName: function (name) { this.Name = name; },

            SetAge: function (age) { this.Age = age; },

            GetName: function () { return this.Name; },

            GetAge: function () { return this.Age; }

        }

       var Alan = new Man("Alan", 25);

       Alan.Name = "Alice"; //悲剧了,我alert的时候变成Alice

       Alan.SetAge(10);//悲剧,被别人把我的年龄给这么小

       alert(Alan.GetName());

       Alan.DisplayAll = function () { return "Name: "+this.GetName() + "; Age: " + this.GetAge() }

       alert(Alan.DisplayAll());

我们发现貌似样子很像C#中的SetterGetter,但是还是可以被外部修改。但是从约束上来看,貌似比上面的code要好看些,通过方法来设置初始值。但是问题还是没有解决,我们来看看下面一种方法:闭包

//我需要解释一下,在Javascript中是通过This关键字来开发权限的(Public)。

在讲闭包之前,我们需要了解下闭包的本质: 在Javascript中,只有方法是有作用域的,如果在方法中声明的变量在外部是无法访问的,那Private的概念就出来了。

var Person = new Interface("Person", ["SetName", "SetAge", "GetName", "GetAge"]);

       var Man = function (newname, newage) {

               var name, age;

               this.SetName = function (newname) { name = newname; }

               this.SetAge = function (newage) { age = newage; }

               this.GetName = function () { return name; }

               this.GetAge = function () { return age; }

               this.SetAge(newage);

               this.SetName(newname);     

       }

       var Alan = new Man("Alan", 25);

       Alan.name="Alice"; //现在nameprivate了,我是无法去修改的

       Alan.SetAge(10); //悲剧,被别人把我的年龄给这么小

      alert(Alan.GetAge());

现在私有的功能就实现了,我们只是用Var来代替了This而已。//我们把公共(Public)并且可以访问Private的方法称为特权方法,比如上面的this.SetName, this.SetAge.

如果我们的公共方法不涉及到访问Private的字段,那我们可以把他们放到Prototype中。//好处是多个实例的时候内存中也只有一分拷贝

Man.prototype.DisplayAll = function () { return "Name: " + this.GetName() + "; Age: " + this.GetAge() }

哈哈~我们来看下稍微有点难度的东西:静态变量和方法

我们都是知道静态的东西属于类(Class),我们来修改下上面的代码:

var Person = new Interface("Person", ["SetName", "SetAge", "GetName", "GetAge","GetCount"]);

      var Man = (function () {

          var count = 0;

          return function (newname, newage) {

              var name, age;

              this.SetName = function (newname) { name = newname; }

              this.SetAge = function (newage) { age = newage; }

              this.GetName = function () { return name; }

              this.GetAge = function () { return age; }

              this.GetCount = function () { return count; }

              this.SetAge(newage);

              this.SetName(newname);

              count++;

          }

      })();

          Man.prototype.DisplayAll = function () { return "Name: " + this.GetName() + "; Age: " + this.GetAge() }

          var Alan1 = new Man("Alan", 25);

          var Alan2 = new Man("Alan", 25);

     alert("There are "+Alan2.GetCount()+" instances of Man" );

不管我们是通过Alan1Alan2GetCount,结果都一样都是2. 这里count就是一个私有的静态变量。

javascript实例教程之封装的用法

标签:

原文地址:http://www.cnblogs.com/maiziedu/p/5524220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!