数据类型检测有哪些方法?

一省:HTML

2. !DOCTYPE的作用?

<!DOCTYPE html>

html文件中,我们常见开头的这一句代码,这句代码究竟有什么作用呢?
doctype是文档类型的意思,<!DOCTYPE html>: 声明文档类型。很久以前,早期的 HTML(大约 1991 年 2 月),文档类型声明类似于链接,规定了 HTML 页面必须遵从的良好规则,能自动检测错误和其他有用的东西,使用如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

然而这种写法已经过时了,这些内容已成为历史。只需要知道 <!DOCTYPE html> 是最短有效的文档声明。—— MDN

二省: CSS

2. css选择器的优先级以及权重?

  1. 优先级
    !important > 行内样式 > id选择器 > 类选择器 == 属性选择器 == 伪类选择器 > 标签选择器== 伪元素选择器 > 通配符选择器 == 继承
  2. 权重
  • !important: ∞无穷大
  • 行内样式:1000
  • id选择器:100
  • 类、属性、伪类选择器: 10
  • 标签、伪元素选择器: 1
  • 通配符、继承:0

三省:JavaScript

2. 数据类型检测有哪些方法?

  1. typeof
console.log(typeof 10); //number
console.log(typeof '10'); //string  
console.log(typeof false); //boolean
console.log(typeof {}); //object
console.log(typeof []); //object
console.log(typeof null); //object
console.log(typeof undefined); //undefined
console.log(typeof (function(){})); //function
console.log(typeof Symbol());// symbol
  1. instanceof
console.log(10 instanceof Number); //false
console.log("10" instanceof String); //false
console.log(false instanceof Boolean);// false
console.log(Symbol() instanceof Symbol)//false
console.log([] instanceof Array); //true
console.log({} instanceof Object); //true
console.log((function(){}) instanceof Function);// true

instanceof无法检测基础数据类型,只能检测引用数据类型。instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

  1. constructor
console.log((10).constructor === Number)//true
console.log(("10").constructor === String)//true
console.log((false).constructor === Boolean)//true
console.log(({}).constructor === Object)//true
console.log(([]).constructor === Array)//true
console.log((function(){}).constructor === Function)//true
console.log(Symbol().constructor === Symbol)//true

注意:对象的constructor是可以被改变的。

  1. Object.prototype.toString()
console.log(Object.prototype.toString.call(10)); // [object String]
console.log(Object.prototype.toString.call("10")) ;    // [object Number]
console.log(Object.prototype.toString.call(true)) ; // [object Boolean]
console.log(Object.prototype.toString.call(Symbol())); //[object Symbol]
console.log(Object.prototype.toString.call(undefined)) ; // [object Undefined]
console.log(Object.prototype.toString.call(null)) ; // [object Null]
console.log(Object.prototype.toString.call(new Function())) ; // [object Function]
console.log(Object.prototype.toString.call([])) ; // [object Array]
console.log(Object.prototype.toString.call({})) ; // [object Object]

toString() 方法返回一个表示该对象的字符串。toString() 可以与每个对象一起使用,并且(默认情况下)允许你获得它的类。以这种方式使用 toString() 是不可靠的;对象可以通过定义 Symbol.toStringTag 属性来更改 Object.prototype.toString() 的行为,从而导致意想不到的结果。例如:

const myDate = new Date();
Object.prototype.toString.call(myDate); // [object Date]

myDate[Symbol.toStringTag] = "myDate";
Object.prototype.toString.call(myDate); // [object myDate]

Date.prototype[Symbol.toStringTag] = "prototype polluted";
Object.prototype.toString.call(new Date()); // [object prototype polluted]

—— MDN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端每日三省

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值