笔记 - 面向对象JavaScript - 100617

cover-object-oriented-javascriptPackt Publishing - Object Oriented JavaScript

Classes

There's a difference between JavaScript and the "classic" OO languages like C++ and Java. You should understand right from the start that in JavaScript there are no classes; everything is based on objects. JavaScript has the notion of prototypes, which are also objects (we'll discuss them later in detail).

In a classic OO language, you'd say something like "create me a new object called Bob which is of class Person". In a prototypal OO language, you'd say, "I'm going to take this object Person that I have lying around and reuse it as a prototype for a new object that I'll call Bob".

In classical OOP, classes inherit from other classes, but in JavaScript, because 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 inherited a number of methods, 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.


收藏与分享

添加评论


Security code
换一张图