Then, prototype can be used to dynamically enhance the object, then some JS primitive class, do not provide the function we want, we can use prototype to enhance it.
Here are some specific cases, I hope you can extrapolate:
<pre name= "Code" class= "JavaScript" >//detects whether the specified element is in the array array.prototype.contains=function (e) {for (I-in-this) {if ( This[i]===e) return true;} Return false;};/ /Test Alert (New Array (' A ', ' B '). Contains (' a ')); var arr = [' red ', ' yellow ', ' Blue '];alert (arr.contains (' Red '));
Tail to Empty string.prototype.trim=function () {return this.replace (/^\s+|\s+$/igm, ');}; Alert ("' abc ' length = = =" + ' ABC ');//measure the length of the empty after the alert ("' ABC ' to empty after the length = = =" + ' ABC '. Trim (). length);
JavaScript Object-Oriented programming (4) prototype use case: Built-in object enhancements