面向对象的JavaScript(OOP)

面向对象的JavaScript(OOP)

JavaScript的类(Classes)和对象(Objects)

In JavaScript, there are no classes; everything is based on objects. JavaScript has the notion(概念) of prototypes, which are also objects.

 

JavaScript封装(Encapsulation)

In compiled languages, you can't actually read the code that makes an object work. In JavaScript, because it's an interpreted language(解释型语言), you can see the source code, but the concept(概念) is still the same—you work with the object's interface, without worrying about its implementation.


Another aspect of information hiding is the visibility(可见性) of methods and properties. In some languages, objects can have public, private, and protected methods and properties. This categorization(分类) defines the level of access the users of the object have. For example, only the methods of the same object have access to the private methods, while anyone has access to the publicones. In JavaScript, all methods and properties are public, but we'll see that there are ways to protect the data inside an object and achieve privacy(隐私).


JavaScript聚合(Aggregation)

Combining(组合) several objects into a new one is known as aggregation(聚合) or composition(组成). It's a powerful way to separate a problem into smaller and more manageable parts (divide and conquer). When a problem scope is so complex that it's impossible to think about it at a detailed level in its entirety, you can separate the problem into several smaller areas, and possibly then separate each of these into even smaller chunks. This allows you to think about the problem on several levels of abstraction. 


To use another analogy(比喻,类比), a Book object can contain (aggregate(合计合并)) one or more Author objects, a Publisher object, several Chapter objects, a TOC(table of contents), 

and so on.


JavaScript继承(Inheritance)

Inheritance is an elegant(优雅的) way to reuse(复用) existing code.

In classical OOP, classes inherit from other classes, but in JavaScript, since there are no classes, objects inherit from other objects.(对象从另一个对象继承而来).

When an object inherits from another object, it usually adds new methods to the inherited ones, thus extending(继承) the old object. Often, the following phrases(短语) can be used interchangeably: "B inherits from A" and "B extends A". Also, the object that inherits can pick one or more methods and redefine them, customizing them for its own needs. This way, the interface stays the same, the method name is the same, but when called on the new object, the method behaves differently. This way of redefining how an inherited method works is known as overriding(覆写).


JavaScript多态(Polymorphism)

You can call the methods Bob.talk(), Jill.talk(), and Jack.talk()and they'll all work fine, albeit(尽管) producing(产生) different results (Bob will probably talk more about performance, Jill about beauty, and Jack about deadlines). Each object inherited the method talkfrom Personand customized it.


附图片一张:

面向对象的JavaScript(OOP)_第1张图片


====END====



你可能感兴趣的:(面向对象的JavaScript(OOP))