Skip to content

二次封装typeof #459

@LittlebearHat

Description

@LittlebearHat
function myType(val) {
  let template = {
    "[object Array]": "array",
    "[object Object]": "object",
  };
  if (val === null) {
    return val;//如果为null则返回自己
  }
  if (typeof val === "object") {//对object进行二次检验
    let type1 = Object.prototype.toString.call(val);
    return template[type1];
  }
  return typeof val;//基本数据类型照常返回
}
console.log(myType([1, 2, 3]), typeof [1, 2, 3]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions