Home  >  Article  >  Web Front-end  >  javascript object-oriented new data encapsulation

javascript object-oriented new data encapsulation

高洛峰
高洛峰Original
2017-01-04 09:19:521071browse

Today we mainly discuss how to implement data encapsulation in JavaScript scripts.
The simple point of data encapsulation is to hide the content that you don’t want the caller to see. It is the first of the three elements of object-oriented programming. The other two are inheritance and polymorphism. They will be discussed later.
Regarding the implementation of data encapsulation, it is implemented through keywords such as public, private, and static in C++, Java, C# and other languages. In JavaScript, it takes a completely different form. Before discussing how to specifically implement a certain form of data encapsulation, let's first talk about a few simple JavaScript concepts that everyone is familiar with but easy to ignore.
1 Several basic concepts
1.1 Variable definition
In the JavaScript language, variables are defined through the var keyword.
But if we directly assign a value to a variable that is not defined using var, then this variable will become a global variable.
Generally, we should avoid using variables that are not defined with var. The main reason is that it will affect the execution efficiency of the program, because accessing global variables is much slower than local variables.
But this usage can ensure that our variables must be global variables.
In addition, in order to ensure speed, when we use global variables, we can define a local variable through var, and then assign the global variable to it, so that we can get a local reference of the global variable.
1.2 Variable type
If there is no defined variable, the type is undefined.
The value of a variable can be a function.
Function can act as a class in JavaScript.
1.3 Variable scope
Variable scope refers to the effective scope of the variable life cycle.
Blocks created simply with { } cannot create scopes.
with adds the scope of the object it contains to the current scope chain, but with does not create a new scope. After the with block ends, the object scope is removed from the current scope chain.
In try-catch, the error object of catch is only valid in the catch block, but the variables defined in the catch block belong to the current scope.
Blocks created by other control statements such as if, for, for-in, while, do-while, switch, etc. cannot create scopes.
A function created with function will create a new scope and add it to the current scope.
2 Encapsulation
Let’s discuss the specific encapsulation. First, let’s talk about the most familiar encapsulations: private instance members, public instance members and public static members. Finally, we will discuss the encapsulation methods of private static members and static classes that are unfamiliar to everyone. Because what we are going to discuss below is object-oriented programming, when functions are defined and used as classes, we will temporarily call them classes.
2.1 Private instance members
Private instance members in JavaScript can actually be implemented using local variables within functions, which are equivalent to private instance members of the class. For example:

class1 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 
// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 
// constructor 
{ 
method1(); 
method2(); 
} 
} 
var o = new class1(); 
// error 
alert(o.m_first); 
o.method1();

Here m_first and m_second are two private instance fields of class1, method1 and method2 are two private instance methods. They can only be used inside objects of this class and cannot be used outside objects.
Here you will find that there are two ways to create a private method, one is to define the method directly in the class, the other is to first define a local variable (private instance field), and then define an anonymous method to assign a value to it.
If you define a method directly in a class, the scope of the method is this class, so this method cannot be accessed outside this class, and it can access all private instance fields in the class, which guarantees This is a private instance method.
The second way to create a private instance method has the same effect as the first way, but the second way is more flexible.
You should also notice that the constructor code in class1 is enclosed in { }. Although this is not necessary, the code looks clearer.
Regarding this constructor code, there are two points that need to be explained:
1. The constructor code must be placed at the end of the entire class definition. This is to ensure that all methods called in it have been is defined. Because JavaScript is an interpreted language, it will be executed in order from top to bottom. Therefore, if the constructor code is placed in front of other method definitions, the method to be called will not be found when the calling statement is executed. Something went wrong.
2. We already know that the block created by { } will not change the scope, so if you create a local variable in such a constructor code, you are actually creating a private instance member in the entire class, so if you need to use For local variables, a private instance method should be defined, for example, it can be named constructor(). In the private instance method of constructor(), define the local variables and the code to be executed in the original { } constructor, and then call it directly at the end of the class. That's it. So a better way to write it is like this:

class1 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 
// private methods 
function constructor() { 
method1(); 
method2(); 
} 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 
constructor(); 
} 
var o = new class1(); 
// error 
alert(o.m_first); 
o.method1();

最后,你可能还会发现 class1 的定义我们没有用 var,这样做我们就可以保证它是个全局的类了。 
2.2 公有实例成员 
公有实例成员可以通过两种方式来创建,我们先来看下面这个例子: 

class2 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 
// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 
// public fields 
this.first = "first"; 
this.second = ['s','e','c','o','n','d']; 

// public methods 
this.method1 = method2; 

this.method2 = function() { 
alert(this.second); 
} 
// constructor 
{ 
method1(); 
method2(); 
} 
} 
// public method 
class1.prototype.method3 = function() { 
alert(this.first); 
} 

var o = new class2(); 

o.method1(); 
o.method2(); 
o.method3(); 
alert(o.first);

我们发现这个例子是在 class1 的例子上做了一些补充。给它添加了公有实例字段和公有实例方法,我们把它们通称为公有实例成员。 
我们应该已经发现,创建公有实例成员其实很简单,一种方式是通过在类中给 this.memberName 来赋值,如果值是函数之外的类型,那就是个公有实例字段,如果值是函数类型,那就是公有实例方法。另外一种方式则是通过给 className.prototype.memberName 赋值,可赋值的类型跟 this.memberName 是相同的。 
到底是通过 this 方式定义好呢,还是通过 prototype 方式定义好呢? 
其实它们各有各的用途,它们之间不是谁比谁更好的关系。在某些情况下,我们只能用其中特定的一种方式来定义公有实例成员,而不能够使用另一种方式。原因在于它们实际上是有区别的: 
1、prototype 方式只应该在类外定义。this 方式只能在类中定义。 
2、prototype 方式如果在类中定义时,则存取私有实例成员时,总是存取最后一个对象实例中的私有实例成员。 
3、prototype 方式定义的公有实例成员是创建在类的原型之上的成员。this 方式定义的公有实例成员,是直接创建在类的实例对象上的成员。 
基于前两点区别,我们可以得到这样的结论:如果要在公有实例方法中存取私有实例成员,那么必须用 this 方式定义。 
关于第三点区别,我们后面在讨论继承时再对它进行更深入的剖析。这里只要知道有这个区别就可以了。 
我们还会发现,公有实例成员和私有实例成员名字是可以相同的,这样不会有冲突吗? 
当然不会。原因在于它们的存取方式不同,公有实例成员在类中存取时,必须要用 this. 前缀来引用。而私有实例成员在类中存取时,不使用也不能够使用 this. 前缀来存取。而在类外存取时,只有公有成员是可以通过类的实例对象存取的,私有成员无法存取。 
2.3 公有静态成员 
公有静态成员的定义很简单,例如: 

class3 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 
// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 
// constructor 
{ 
method1(); 
method2(); 
} 
} 

// public static field 
class3.field1 = 1; 

// public static method 
class3.method1 = function() { 
alert(class3.field1); 
} 

class3.method1();

这个例子的 class3 跟 class1 很像。不同的是 class3 的外面,我们又给 class3 定义了一个静态字段和静态方法。 
定义的方式就是给 className.memberName 直接赋值。 
这里定义的静态字段和静态方法都是可以被直接通过类名引用来存取的,而不需要创建对象。因此它们是公有静态成员。 
不过有点要记住,一定不要将公有静态成员定义在它所在的类的内部,否则你会得到非你所期望的结果。我们可以看下面这个例子: 

class4 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 

var s_second = 2; 

// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 
class4.method1 = function() { 
s_second++; 
} 
class4.method2 = function() { 
alert(s_second); 
} 
} 
var o1 = new class4(); 
class4.method2(); // 2 
class4.method1(); 
class4.method2(); // 3 
var o2 = new class4(); 
class4.method2(); // 2 
class4.method1(); 
class4.method2(); // 3

这个例子中,我们期望 s_second 能够扮演一个私有静态成员的角色,但是输出结果却不是我们所期望的。我们会发现 s_second 实际上是 class4 的一个私有实例成员,而不是私有静态成员。而 class4 的 method1 和 method2 所存取的私有成员总是类的最后一个实例对象中的这个私有实例成员。 
问题出在哪儿呢? 
问题出在每次通过 new class4() 创建一个对象实例时,class4 中的所有语句都会重新执行,因此,s_second 被重置,并成为新对象中的一个私有实例成员。而 class4.method1 和 class4.method2 也被重新定义了,而这个定义也将它们的变量作用域切换到了最后一个对象上来。这与把通过 prototype 方式创建的公有实例方法定义在类的内部而产生的错误是一样的。 
所以,一定不要将公有静态成员定义在它所在的类的内部!也不要把通过 prototype 方式创建的公有实例方法定义在类的内部! 
那如何定义一个私有静态成员呢? 
2.4 私有静态成员 
前面在基本概念里我们已经清楚了,只有用 function 创建函数,才能创建一个新的作用域,而要创建私有成员(不论是静态成员,还是实例成员),都需要通过创建新的作用域才能够起到数据隐藏的目的。下面所采用的方法就是基于这一点来实现的。 
实现私有静态成员是通过创建一个匿名函数函数来创建一个新的作用域来实现的。 
通常我们使用匿名函数时都是将它赋值给一个变量,然后通过这个变量引用该匿名函数。这种情况下,该匿名函数可以被反复调用或者作为类去创建对象。而这里,我们创建的匿名函数不赋值给任何变量,在它创建后立即执行,或者立即实例化为一个对象,并且该对象也不赋值给任何变量,这种情况下,该函数本身或者它实例化后的对象都不能够被再次存取,因此它唯一的作用就是创建了一个新的作用域,并隔离了它内部的所有局部变量和函数。因此,这些局部变量和函数就成了我们所需要的私有静态成员。而这个立即执行的匿名函数或者立即实例化的匿名函数我们称它为静态封装环境。 
下面我们先来看通过直接调用匿名函数方式来创建带有私有静态成员的类的例子: 

class5 = (function() { 
// private static fields 
var s_first = 1; 
var s_second = 2; 

// private static methods 
function s_method1() { 
s_first++; 
} 
var s_second = 2; 

function constructor() { 
// private fields 
var m_first = 1; 
javascript面向对象全新理练(二) 
var m_second = 2; 

// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 

// public fields 
this.first = "first"; 
this.second = ['s','e','c','o','n','d']; 

// public methods 
this.method1 = function() { 
s_second--; 
} 

this.method2 = function() { 
alert(this.second); 
} 

// constructor 
{ 
s_method1(); 
this.method1(); 
} 
} 
// public static methods 
constructor.method1 = function() { 
s_first++; 
alert(s_first); 
} 
constructor.method2 = function() { 
alert(s_second); 
} 

return constructor; 
})(); 

var o1 = new class5(); 
class5.method1(); 
class5.method2(); 
o1.method2(); 
var o2 = new class5(); 
class5.method1(); 
class5.method2(); 
o2.method2(); 
这个例子中,通过 
(function() { 
... 
function contructor () { 
... 
} 
return constructor; 
})();

来创建了一个静态封装环境,实际的类是在这个环境中定义的,并且在最后通过 return 语句将最后的类返回给我们的全局变量 class5,然后我们就可以通过 class5 来引用这个带有静态私有成员的类了。 
为了区分私有静态成员和私有实例成员,我们在私有静态成员前面用了 s_ 前缀,在私有实例成员前面加了 m_ 前缀,这样避免了重名,因此在对象中总是可以存取私有静态成员的。 
但是这种命名方式不是必须的,只是推荐的,私有静态成员可以跟私有实例成员同名,在重名的情况下,在类构造器和在类中定义的实例方法中存取的都是私有实例成员,在静态方法(不论是公有静态方法还是私有静态方法)中存取的都是私有静态成员。 
在类外并且在静态封装环境中通过 prototype 方式定义的公有实例方法存取的是私有静态成员。 
在静态封装环境外定义的公有静态方法和通过 prototype 方式定义的公有实例方法无法直接存取私有静态成员。 
另外一种方式通过直接实例化匿名函数方式来创建带有私有静态成员的类的例子跟上面的例子很相似: 

new function() { 
// private static fields 
var s_first = 1; 
var s_second = 2; 

// private static methods 
function s_method1() { 
s_first++; 
} 
var s_second = 2; 

class6 = function() { 
// private fields 
var m_first = 1; 
var m_second = 2; 

// private methods 
function method1() { 
alert(m_first); 
} 
var method2 = function() { 
alert(m_second); 
} 

// public fields 
this.first = "first"; 
this.second = ['s','e','c','o','n','d']; 

// public methods 
this.method1 = function() { 
s_second--; 
} 

this.method2 = function() { 
alert(this.second); 
} 

// constructor 
{ 
s_method1(); 
this.method1(); 
} 
} 
// public static methods 
class6.method1 = function() { 
s_first++; 
alert(s_first); 
} 
class6.method2 = function() { 
alert(s_second); 
} 
}; 

var o1 = new class6(); 
class6.method1(); 
class6.method2(); 
o1.method2(); 
var o2 = new class6(); 
class6.method1(); 
class6.method2(); 
o2.method2();

这个例子的结果跟通过第一种方式创建的例子是相同的。只不过它的静态封装环境是这样的: 
new function() { 
... 
}; 
在这里,该函数没有返回值,并且对于 class5 的定义是直接在静态封装环境内部通过给一个没有用 var 定义的变量赋值的方式实现的。 
当然,也完全可以在 
(function() { 
... 
})(); 
这种方式中,不给该函数定义返回值,而直接在静态封装环境内部通过给一个没有用 var 定义的变量赋值的方式来实现带有私有静态成员的类的定义。 
这两种方式在这里是等价的。 
2.5 静态类 
所谓的静态类,是一种不能够被实例化,并且只包含有静态成员的类。 
在 JavaScript 中我们通过直接实例化一个匿名函数的对象,就可以实现静态类了。例如: 

class7 = new function() { 
// private static fields 
var s_first = 1; 
var s_second = 2; 
// private static method 
function method1() { 
alert(s_first); 
} 
// public static method 
this.method1 = function() { 
method1(); 
alert(s_second); 
} 
} 
class7.method1();

大家会发现,class7 其实就是个对象,只不过这个对象所属的是匿名类,该类在创建完 class7 这个对象后,就不能再被使用了。而 class7 不是一个 function,所以不能够作为一个类被实例化,因此,这里它就相当于一个静态类了。

更多javascript 面向对象全新理练之数据的封装相关文章请关注PHP中文网!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn