Unable to execute Doug Crockford deentityify example in jsfiddle

I am trying to execute this really good deentityify example in the Douglas Crockfords J-TBP book using jsfiddle

String.method('deentityify', function() { // The entity table. It maps entity names to // characters. var entity = { quot: '"', lt: '<', gt: '>' }; // Return the deentityify method. return function() { // This is the deentityify method. It calls the string // replace method, looking for substrings that start // with '&' and end with ';'. If the characters in // between are in the entity table, then replace the // entity with the character from the table. It uses // a regular expression (Chapter 7). return this.replace(/&([^&;]+);/g, function(a, b) { var r = entity[b]; return typeof r === 'string' ? r : a; }); }; }()); document.writeln('&lt;&quot;&gt;'.deentityify()); // <"> 
+4
source share
1 answer

This piece of code depends on a bit of sugar, namely on the method method , which you must determine in advance. (This is described at the beginning of the book). An online copy and explanation of this is in the Sugar section of Crockford's article, Classical JavaScript Inheritance . It looks like this:

 Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; 

The adjusted version of your script, including the one above, is located at http://jsfiddle.net/W9Ncd/ .

+8
source

Source: https://habr.com/ru/post/1446319/


All Articles