JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

9.8.4 Preventing Class Extensions

9.8.4 Preventing Class Extensions

It is usually considered a feature of JavaScript that classes can be dynamically extended by adding new methods to the prototype object. ECMAScript 5 allows you to prevent this, if you want to. Object.preventExtensions() makes an object nonextensible ( §6.8.3 ), which means that no new properties can be added to it. Object.seal() takes this a step further: it prevents the addition of new properties and also makes all current properties nonconfigurable, so that they cannot be deleted. (A nonconfigurable

9.8 Classes in ECMAScript 5 | 241

property can still be writable, however, and can still be converted into a read-only property.) To prevent extensions to Object.prototype, you can simply write:


Another dynamic feature of JavaScript is the ability to replace (or “monkey-patch”) methods of an object:

var original_sort_method = Array.prototype.sort;

Array.prototype.sort = function() {

var start = new Date();

original_sort_method.apply(this, arguments);

var end = new Date();

console.log("Array sort took " + (end - start) + " milliseconds.");


You can prevent this kind of alteration by making your instance methods read-only. The freezeProps() utility function defined above is one way to accomplish this. Another way is with Object.freeze(), which does everything that Object.seal()does, but also makes all properties read-only and nonconfigurable.

There is a feature of read-only properties that is important to understand when working with classes. If an object oinherits a read-only property p, an attempt to assign to o.p will fail and will not create a new property in o. If you want to override an inherited read-only property, you have to use Object.defineProperty() or Object.defineProperties()or Object.create() to create the new property. This means that if you make the instance methods of a class read-only, it becomes significantly more difficult for subclasses to override those methods.

It is not usually necessary to lock down prototype objects like this, but there are some circumstances where preventing extensions to an object can be useful. Think back to the enumeration() class factory function of Example 9-7 . That function stored the instances of each enumerated type in properties of the constructor object, and also in the values array of the constructor. These properties and array serve as the official list of instances of the enumerated type, and it is worth freezing them, so that new instances cannot be added and existing instances cannot be deleted or altered. In the enumeration() function we can simply add these lines of code:

Object.freeze(enumeration.values); Object.freeze(enumeration);

Notice that by calling Object.freeze() on the enumerated type, we prevent the future use of the objectId property defined in Example 9-17 . A solution to this problem is to read the objectId property (calling the underlying accessor method and setting the internal property) of the enumerated type once before freezing it.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1