当前位置: 首页 > wzjs >正文

做网站时如何写接口文档江苏省水利工程建设局网站

做网站时如何写接口文档,江苏省水利工程建设局网站,湖南响应式网站建设,室内设计公司简介注:纯手打,如有错误欢迎评论区交流! 转载请注明出处:https://blog.csdn.net/testleaf/article/details/147954653 编写此文是为了更好地学习前端知识,如果损害了有关人的利益,请联系删除! 本文章…

注:纯手打,如有错误欢迎评论区交流!
转载请注明出处:https://blog.csdn.net/testleaf/article/details/147954653
编写此文是为了更好地学习前端知识,如果损害了有关人的利益,请联系删除!
本文章将不定时更新,敬请期待!!!
欢迎点赞、收藏、转发、关注,多谢!!!

目录

    • 一、typeof:判断基本类型
    • 二、instanceof:判断实例原型链
    • 三、Object.prototype.toString.call():终极方案
    • 四、专门方法判断特定类型
      • 1、判断数组
      • 2、判断 NaN
      • 3、判断 null 或 undefined
    • 五、ES6+ 新增类型判断
      • 1、Map/Set/WeakMap/WeakSet
      • 2、Promise
      • 3、自定义类
    • 六、特殊案例处理
      • 1、区分对象和数组
      • 2、判断纯对象(Plain Object)
    • 七、终极方案对比表
    • 八、实际应用示例
    • 九、总结

一、typeof:判断基本类型

​​特点​​:能识别基本类型(除 null 外),但对引用类型返回 object

typeof 'hello'      // 'string'
typeof 42           // 'number'
typeof true         // 'boolean'
typeof undefined    // 'undefined'
typeof Symbol()     // 'symbol'
typeof BigInt(10)   // 'bigint'
typeof function(){} // 'function'// 局限性
typeof null         // 'object' (历史遗留问题)
typeof []           // 'object'
typeof {}           // 'object'

二、instanceof:判断实例原型链

​​特点​​:检查对象是否是某个构造函数的实例(对基本类型无效)。

[] instanceof Array      // true
{} instanceof Object     // true
new Date() instanceof Date // true// 局限性
'abc' instanceof String  // false (基本类型不适用)
null instanceof Object   // false

三、Object.prototype.toString.call():终极方案

​​特点​​:精准识别所有类型(推荐)。

Object.prototype.toString.call('hello')   // '[object String]'
Object.prototype.toString.call(42)        // '[object Number]'
Object.prototype.toString.call(true)      // '[object Boolean]'
Object.prototype.toString.call(null)      // '[object Null]'
Object.prototype.toString.call(undefined) // '[object Undefined]'
Object.prototype.toString.call([])        // '[object Array]'
Object.prototype.toString.call({})        // '[object Object]'
Object.prototype.toString.call(/regex/)   // '[object RegExp]'
Object.prototype.toString.call(new Date())// '[object Date]'

封装成通用函数​​:

function getType(obj) {return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}
getType([]) // 'array'
getType(null) // 'null'

四、专门方法判断特定类型

1、判断数组

Array.isArray([]) // true

2、判断 NaN

Number.isNaN(NaN) // true (注意:全局 isNaN() 会先尝试转数字)

3、判断 null 或 undefined

value === null      // 仅判断 null
value === undefined // 仅判断 undefined
value == null       // 同时判断 null 或 undefined

五、ES6+ 新增类型判断

1、Map/Set/WeakMap/WeakSet

Object.prototype.toString.call(new Map())    // '[object Map]'
Object.prototype.toString.call(new Set())    // '[object Set]'

2、Promise

Object.prototype.toString.call(Promise.resolve()) // '[object Promise]'

3、自定义类

class MyClass {}
Object.prototype.toString.call(new MyClass()) // '[object Object]' (需自定义 toStringTag)

六、特殊案例处理

1、区分对象和数组

function isObject(obj) {return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
}

2、判断纯对象(Plain Object)

function isPlainObject(obj) {return Object.prototype.toString.call(obj) === '[object Object]' &&Object.getPrototypeOf(obj) === Object.prototype;
}
isPlainObject({})     // true
isPlainObject([])     // false
isPlainObject(new Date()) // false

七、终极方案对比表

方法适用场景局限性
typeof基本类型(除 nullnull 返回 object
instanceof引用类型(检查构造函数)不适用于基本类型
Object.prototype.toString​​所有类型​​(最精准)
Array.isArray()仅判断数组仅适用于数组
=== null 或 === undefined精确判断 null/undefined仅适用于这两个值

八、实际应用示例

function typeCheck(value) {const type = Object.prototype.toString.call(value).slice(8, -1);switch (type) {case 'String':case 'Number':case 'Boolean':case 'Null':case 'Undefined':case 'Symbol':case 'BigInt':return type.toLowerCase();default:return type; // 'Array', 'Object', 'Date', 'RegExp' 等}
}typeCheck(null)      // 'null'
typeCheck([])        // 'Array'
typeCheck(new Date())// 'Date'

九、总结

1、​​基本类型​​:优先用 typeof(注意 null 的坑)。
​​2、引用类型​​:用 Object.prototype.toString.call()
​​3、数组​​:直接用 Array.isArray()
4、​​特殊值​​:nullundefined=== 严格判断。
5、​​ES6+ 类型​​:结合 toString 和专用方法(如 Promise.resolve())。

http://www.dtcms.com/wzjs/566600.html

相关文章:

  • 专业seo网站优化公司百度广告费用
  • 最简单的手机网站制作如何用服务器做网站
  • 四川城乡住房和城乡建设厅网站首页有限责任公司破产法人承担的责任
  • 网站建设选天祥免费创立网站
  • 网站手机版绑定域名域名注册 腾讯云
  • dns 本地 网站建设wordpress 视频网站
  • 网站建设的分阶段步骤贵州建设学校网站
  • 浙江建设继续教育网站一个网站如何产生流量
  • 网站建设定制价格明细表东莞网站建设方案外包
  • 海南建设监理协会网站网站建设人才招聘
  • 威海高端网站建设重庆彼客的网站是谁家做的
  • 制作类网站网络推广业务员是干什么的
  • angular做的网站seo查询5118
  • 新乡哪有做网站的做面食专业网站
  • 支付宝网站接口申请淘宝建设网站的
  • 云浮住房和城乡建设部官方网站精准营销系统价值
  • 本地常州网站建设长兴县建设局网站
  • 海淘网站开发Dw制作个人网站
  • 上海专业网站建设报价单系统管理的主要内容
  • 网站设计论文经济可行性分析软件开发的工作内容
  • 目前做网站最好的语言是h5企业网站定制排名
  • 智慧团建信息系统网站登录织梦瀑布流网站模板
  • 深圳网站搜索排名公司网站备案材料
  • 深圳美容网站建设百度如何搜索关键词
  • l临沂互助系统网站开发石家庄seo网络推广
  • html5网站建设企业湖北网站建设详细方案
  • 网站建设门店牌子前程无忧网站开发待遇怎么样
  • 大庆开发网站公司app网站建设工作师
  • 模仿ios系统的html网站网站内容管理平台
  • 芜湖有哪些招聘网站蓝色手机网站模板