如何在Javascript中利用封装这个特性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Javascript中利用封装这个特性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3868字,纯文字阅读大概需要6分钟。
内容图文
![如何在Javascript中利用封装这个特性](/upload/InfoBanner/zyjiaocheng/1134/8747612576664950a8220f72ffeaa8a3.jpg)
对于熟悉C#和Java的兄弟们,面向对象的三大思想(封装,继承,多态)肯定是了解的,那么如何在Javascript中利用封装这个特性呢?
我们会把现实中的一些事物抽象成一个Class并且把事物的属性(名词)作为Class的Property把事物的动作(动词)作为Class的methods。在面向对象的语言中(C#等)都会有一些关键字来修饰类或者属性(Private,public,protect),这些关键词描述了访问的权限,不多做解释。泗阳县民用航空局
我们来看看Javascript的易变的特性(我们还用上一次的例子):
01
|
var Man = function (name, age) { |
02
|
this
.Name = name;
|
03
|
this
.Age = age;
|
04
|
}
|
05
|
var Person = new Interface( "Person" , [ "GetName" , "GetAge" ]); |
06
|
Man.prototype = { GetName:
function () { return this .Name; }, |
07
|
GetAge:
function () { return this .Age; } |
08
|
}
|
09
|
var Gonn = new Man( "Gonn" , 25); |
10
|
alert(Gonn.GetAge());
|
11
|
Gonn.DisplayAll =
function () { return "Name: " + this .GetName() + "; Age: " + this .GetAge() } |
12
|
alert(Gonn.DisplayAll());
|
先创建了一个Class(Javascript的匿名方法)拥有2个公共的(public)的字段(本篇blog会详细讲解,继续往下看)和2个public的方法,我们创建了一个Instance--Gonn,但是我可以为这个Instance动态的添加一个DisplayAll的方法,我想任何面向对象的语言是做不到这一点的,Javascript的灵活体现之一。
现在假设一个场景,如果有很多的程序员要用这段代码,由于Javascript的易变性,程序员就可以在实例化后改变Name的值,那初始化的动作就没有意义了:
1
|
var Gonn = new Man( "Gonn" , 25); |
2
|
Gonn.Name =
"Alice"
;
|
3
|
alert(Gonn.GetName());
|
所以我们不能让外部的人去任意的修改这个字段,在Java或C#中我们只需要个这个字段改为Private,就万事OK了,但是Javascript没有这个关键词,那我们需要这么做呢。我们可以想下在C#除了设置Private之外我们还可以怎么做?我们可以设置Setter和Getter方法。
我们来修改下上面的代码:我们称方法一:
01
|
var Person = new Interface( "Person" , [ "SetName" , "SetAge" , "GetName" , "GetAge" ]); |
02
|
var Man = function (name, age) { |
03
|
this
.SetAge(age);
|
04
|
this
.SetName(name);
|
05
|
}
|
06
|
Man.prototype = {
|
07
|
SetName:
function (name) { this .Name = name; }, |
08
|
SetAge:
function (age) { this .Age = age; }, |
09
|
GetName:
function () { return this .Name; }, |
10
|
GetAge:
function () { return this .Age; } |
11
|
}
|
12
|
var Alan = new Man( "Alan" , 25); |
13
|
Alan.Name =
"Alice"
;
//悲剧了,我alert的时候变成Alice了
|
14
|
Alan.SetAge(10);
//悲剧,被别人把我的年龄给这么小
|
15
|
alert(Alan.GetName());
|
16
|
Alan.DisplayAll =
function () { return "Name: " + this .GetName() + "; Age: " + this .GetAge() } |
17
|
alert(Alan.DisplayAll());
|
貌似样子很像C#中的Setter和Getter,但是还是可以被外部修改。但是从约束上来看,貌似比上面的code要好看些,通过方法来设置初始值。但是问题还是没有解决,我们来看看下面一种方法:闭包。解释一下,在Javascript中是通过This关键字来开发权限的(Public)。在讲闭包之前,我们需要了解下闭包的本质: 在Javascript中,只有方法是有作用域的,如果在方法中声明的变量在外部是无法访问的,那Private的概念就出来了。
01
|
var Person = new Interface( "Person" , [ "SetName" , "SetAge" , "GetName" , "GetAge" ]); |
02
|
var Man = function (newname, newage) { |
03
|
var name, age; |
04
|
this
.SetName =
function (newname) { name = newname; } |
05
|
this
.SetAge =
function (newage) { age = newage; } |
06
|
this
.GetName =
function () { return name; } |
07
|
this
.GetAge =
function () { return age; } |
08
|
this
.SetAge(newage);
|
09
|
this
.SetName(newname);
|
10
|
}
|
11
|
var Alan = new Man( "Alan" , 25); |
12
|
Alan.name=
"Alice"
;
//现在name是private了,我是无法去修改的
|
13
|
Alan.SetAge(10);
|
14
|
alert(Alan.GetAge());
|
现在私有的功能就实现了,我们只是用Var来代替了This而已。//我们把公共(Public)并且可以访问Private的方法称为特权方法,比如上面的this.SetName, this.SetAge.
如果我们的公共方法不涉及到访问Private的字段,那我们可以把他们放到Prototype中。//好处是多个实例的时候内存中也只有一分拷贝
1
|
Man.prototype.DisplayAll =
function () { return "Name: " + this .GetName() + "; Age: " + this .GetAge() } |
哈哈~我们来看下稍微有点难度的东西:静态变量和方法。
我们都是知道静态的东西属于类(Class),我们来修改下上面的代码:
01
|
var Person = new Interface( "Person" , [ "SetName" , "SetAge" , "GetName" , "GetAge" , "GetCount" ]); |
02
|
var Man = ( function () { |
03
|
var count = 0; |
04
|
return function (newname, newage) { |
05
|
var name, age; |
06
|
this
.SetName =
function (newname) { name = newname; } |
07
|
this
.SetAge =
function (newage) { age = newage; } |
08
|
this
.GetName =
function () { return name; } |
09
|
this
.GetAge =
function () { return age; } |
10
|
this
.GetCount =
function () { return count; } |
11
|
this
.SetAge(newage);
|
12
|
this
.SetName(newname);
|
13
|
count++;
|
14
|
}
|
15
|
})();
|
16
|
Man.prototype.DisplayAll =
function () { return "Name: " + this .GetName() + "; Age: " + this .GetAge() } |
17
|
var Alan1 = new Man( "Alan" , 25); |
18
|
var Alan2 = new Man( "Alan" , 25); |
19
|
alert(
"There are "
+Alan2.GetCount()+
" instances of Man" ); |
不管我们是通过Alan1或Alan2去GetCount,结果都一样都是2. 这里count就是一个私有的静态变量。
原文:http://www.cnblogs.com/xiaoyang002/p/4035788.html
内容总结
以上是互联网集市为您收集整理的如何在Javascript中利用封装这个特性全部内容,希望文章能够帮你解决如何在Javascript中利用封装这个特性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。