(D.克罗克福德)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(D.克罗克福德)相关的知识,希望对你有一定的参考价值。

Remedial javascript As Proposed by Douglas Crockford (http://javascript.crockford.com/remedial.html)
  1. /*
  2.  Author: Douglas Crockford
  3.  Source: http://javascript.crockford.com
  4.  Description:
  5.  
  6.   Remedial Javascript As Proposed by Douglas Crockford (http://javascript.crockford.com/remedial.html)
  7.  
  8. */
  9.  
  10. var Remedial = (function(){
  11.  
  12. String.prototype.entityify = function () {
  13. return this.replace(/&/g, "&amp;").replace(/</g,
  14. "&lt;").replace(/>/g, "&gt;");
  15. };
  16.  
  17. String.prototype.quote = function () {
  18. var c, i, l = this.length, o = '"';
  19. for (i = 0; i < l; i += 1) {
  20. c = this.charAt(i);
  21. if (c >= ' ') {
  22. if (c === '\' || c === '"') {
  23. o += '\';
  24. }
  25. o += c;
  26. } else {
  27. switch (c) {
  28. case '':
  29. o += '\b';
  30. break;
  31. case 'f':
  32. o += '\f';
  33. break;
  34. case ' ':
  35. o += '\n';
  36. break;
  37. case ' ':
  38. o += '\r';
  39. break;
  40. case ' ':
  41. o += '\t';
  42. break;
  43. default:
  44. c = c.charCodeAt();
  45. o += '\u00' + Math.floor(c / 16).toString(16) +
  46. (c % 16).toString(16);
  47. }
  48. }
  49. }
  50. return o + '"';
  51. };
  52.  
  53. String.prototype.supplant = function (o) {
  54. return this.replace(/{([^{}]*)}/g,
  55. function (a, b) {
  56. var r = o[b];
  57. return typeof r === 'string' || typeof r === 'number' ? r : a;
  58. }
  59. );
  60. };
  61.  
  62. String.prototype.trim = function () {
  63. return this.replace(/^s+|s+$/g, "");
  64. };
  65.  
  66. // Do whatever you want here
  67. var R = {
  68. typeOf: function(value) {
  69. var s = typeof value;
  70. if (s === 'object') {
  71. if (value) {
  72. if (typeof value.length === 'number' &&
  73. !(value.propertyIsEnumerable('length')) &&
  74. typeof value.splice === 'function') {
  75. s = 'array';
  76. }
  77. } else {
  78. s = 'null';
  79. }
  80. }
  81. return s;
  82. },
  83. isEmpty: (o) {
  84. var i, v;
  85. if (typeOf(o) === 'object') {
  86. for (i in o) {
  87. v = o[i];
  88. if (v !== undefined && typeOf(v) !== 'function') {
  89. return false;
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95.  
  96. }
  97. return R;
  98. })();

以上是关于(D.克罗克福德)的主要内容,如果未能解决你的问题,请参考以下文章

他十年前的书在前端圈人手一本,豆瓣评分9.1。现在,王者归来了!

寄生式继承

原形式继承

使用 jshint 遵守最大长度设置

JavaScript之面向对象学九(原型式继承和寄生式继承)

Vim上下文突出显示