javascript - Does Crockford's "deentityify" method in The Good Parts actually work? -


this particular code has been asked twice before, never question raised "does anything?". appears in page 41 of crockford's book.

here syntactic sugar earlier in book:

object.prototype = function (name, func){     if(!this.prototype[name]){     this.prototype[name] = func;     } 

and here deentityify methody:

string.method('deentityify', function ( ) {     var entity = {         quot: '"',         lt: '<',         gt: '>'     };     return function () {         return this.replace(             /&([^&;]+);/g,             function (a, b) {                 var r = entity[b];                 return typeof r === 'string' ? r : a;             }         );     }; }()); 

if in editor write like:

document.writeln( '&lt;&quot;&gt;'.deentityify( )); 

you'll see in browser: however, if type in browser: document.writeln( '&lt;&quot;&gt;');

you'll still see:

<">

i discovered method isn't replacing entities when attempted use when pre-filling form data setting value field this:

 var venueinput = document.createelement("input");                    ...         venuename = '&lt;&gt;&quot'; // example         venueinput.value = venuename.deentityify();         form.appendchild(venuename);         form.appendchild(venueinput); 

alert show entities aren't being replaced.

can me see i'm doing wrong? thank you!

after fixing mistakes of code shared works charm:

object.prototype.method = function (name, func){      if(!this.prototype[name]){          this.prototype[name] = func;      }  }      string.method('deentityify', function ( ) {      var entity = {          quot: '"',          lt: '<',          gt: '>'      };      return function() {          return this.replace(              /&([^&;]+);/g,              function (a, b) {                  var r = entity[b];                  return typeof r === 'string' ? r : a;              }          );      };  }());      var venueinput = document.createelement("input");    var label = document.createelement("label");  venuename = '&lt;&gt;&quot;'; // example  venueinput.value = venuename.deentityify();  label.innerhtml = venuename;  document.getelementbyid('form').appendchild(label);  document.getelementbyid('form').appendchild(venueinput);
<div id="form"></div>


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -