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

wordpress购物网站/青岛谷歌推广

wordpress购物网站,青岛谷歌推广,十大免费跨境网站,成武菏泽网站建设在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、函数、对象来说,其关系错综复杂,…

在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、函数、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。

要想区别对象、数组、函数单纯使用 typeof 是不行的。或者你会想到 instanceof 方法,例如下面这样:

var a = {};
var b = [];
var c = function () {};//a b c 都是 Object 的实例
console.log(a instanceof Object) //true
console.log(b instanceof Object) //true
console.log(c instanceof Object) //true//只有 Array 类型的 b 才是 Array 的实例
console.log(a instanceof Array) //false
console.log(b instanceof Array) //true
console.log(c instanceof Array) //false//只有 Function 类型的 c 才是 Function 的实例
console.log(a instanceof Function) //false
console.log(b instanceof Function) //false
console.log(c instanceof Function) //true

从以上代码来看,要判断复合数据类型,可以如下判断:

//对象
(a instanceof Object) && !(a instanceof Array) && !(a instanceof Function)
//数组
(a instanceof Object) && (a instanceof Array)
//函数
(a instanceof Object) && (a instanceof Function)

更简便的方式,即是使用 Object.prototype.toString.call() 来确定类型,ECMA 5.1 中关于该方法的描述[1]是这样的:

When the toString method is called, the following steps are taken:
If the this value is undefined, return “[object Undefined]“.
If the this value is null, return “[object Null]“.
Let O be the result of calling ToObject passing the this value as the argument.
Let class be the value of the [[Class]] internal property of O.
Return the String value that is the result of concatenating the three Strings “[object ", class, and "]“.

toString方法被调用时,会执行下面的操作步骤:

  1. 如果this的值为undefined,则返回"[object Undefined]".
  2. 如果this的值为null,则返回"[object Null]".
  3. O成为调用ToObject(this)的结果.
  4. class成为O的内部属性[[Class]]的值.
  5. 返回三个字符串"[object ", class, 以及 "]"连接后的新字符串.

所有内置对象的[[Class]]属性的值是由本规范定义的.所有宿主对象的[[Class]]属性的值可以是除了"Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", "String"之外的的任何字符串.[[Class]]内部属性是引擎内部用来判断一个对象属于哪种类型的值的.

  由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:

console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]

所有类型都会得到不同的字符串,几乎完美。

[1] Object.prototype.toString ()

转载于:https://www.cnblogs.com/onflying/archive/2013/04/18/3029123.html

http://www.jmfq.cn/news/5227471.html

相关文章:

  • 遵义网帮你分类信息网/杭州seo推广排名稳定
  • 购物网站数据分析/电商平台如何推广运营
  • 成都网站建设木木科技/企业网站建设流程
  • 义乌制作网站/seo推广要多少钱
  • 西安市建设协会网站/地推平台去哪里找
  • 深圳靠谱的电商公司/搜索引擎优化 简历
  • 六安市建设银行网站/dw软件怎么制作网页
  • 网站访问速度优化/免费建立个人网站
  • wordpress删除rss/东莞网络推广及优化
  • 建筑网站源码/合肥网站排名推广
  • web用框架做网站/西安百度竞价代运营
  • 网络营销有什么/seo专员岗位职责
  • wordpress 比较/东莞seo计费
  • 天津中冀建设集团有限公司网站/足球比赛统计数据
  • 自动生成作文的网站/巨量引擎广告投放平台代理
  • 上海商地网站建设公司/关键词歌曲免费听
  • 医生咨询在线24小时免费/seo实战培训视频
  • 旅游网站开发的流程图/广州推广服务
  • 哪些网站可以做微课/全网营销平台有哪些
  • 做付费下载的网站/企业网站推广的方法有哪些
  • 网站上可以做直播吗/爱站seo工具包下载
  • 做全套的成都网站/全球网站排名查询
  • 合肥做网站价格/优化网络
  • 淘宝怎么做引流和推广/百中搜优化软件靠谱吗
  • 网站做301根目录在哪/免费建网站哪家好
  • 今日新冠疫情最新情况/baiduseoguide
  • 重庆交易网站建设/怎样开网站
  • 有没有做任务赚钱的网站/互联网运营推广
  • 有没有学做蛋糕的网站和视频/网络营销的工具和方法
  • 怎样做网站ppt/更厉害的病毒2024