手写 trim(去掉字符串首尾空白)

标题里的 thrim 是 typo,应为 trim。下面给出原版以及标准做法。

String.prototype.myTrim = function () {
  return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
};

// 深度遍历对象,对每个字符串属性做 trim
function trimDeep(obj) {
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      if (typeof obj[key] === "object" && obj[key] !== null) {
        trimDeep(obj[key]);
      } else if (typeof obj[key] === "string") {
        obj[key] = obj[key].myTrim();
      }
    }
  }
}

补充

补充:原版的 iTrim 变量名拼写也不一致(标题/方法名都写成了 thrim),同时 for...in 拿到的 i 实际是字符串 key,对 obj[i] 的递归判断要保留 typeof obj[i] === "object" 来区分数组/对象。