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

门户网站建设推荐百度seo教程视频

门户网站建设推荐,百度seo教程视频,地产网站模板,东莞系统网站建设目录 一、typeof 二、instanceof 三、constructor 四、Object.prototype.toString.call() Object.prototype.toString.call(obj)类型检测原理 五、__proto__ 六、 其他 一、typeof typeof在对值类型number、string、boolean 、symbol、 undefined、 function的反应是精准…

目录

一、typeof

二、instanceof

三、constructor

四、Object.prototype.toString.call()

Object.prototype.toString.call(obj)类型检测原理

五、__proto__

六、 其他


一、typeof

  1. typeof在对值类型numberstringboolean symbol、 undefined、 function的反应是精准的;
  2. 但对于对象{ } 、数组[ ] 、null 都会返回 object

console.log(typeof ""); // string
console.log(typeof 1); // number
console.log(typeof NaN); // number
console.log(typeof true); // boolean
console.log(typeof Symbol(1)) // "symbol"
console.log(typeof undefined); // undefined
console.log(typeof function(){}); // functionconsole.log(typeof null); // object   (巨坑...)
console.log(typeof []); // object
console.log(typeof {}); //object

二、instanceof

instanceof可以正确判断对象的类型,其内部运行机制是判断在其原型链中能否找到该类型的原型

  1. 用 instanceof 判断一个实例是否属于某种类型

  2. instanceof 运算符只能正确判断引用数据类型,而不能判断基本数据类型
// 检测构造函数B的原型是否有出现在对象A的原型链上。
A instanceof B [] instanceof Array // true
[].__proto__ == Array.prototype // trueconsole.log([] instanceof Array); // true
console.log([] instanceof Object); // trueconsole.log({} instanceof Object); //trueconsole.log(function(){} instanceof Function); // true
console.log(function(){} instanceof Object); // trueconsole.log((new Number(1)) instanceof Number); // true
console.log((new Number(1)) instanceof Object); // true
//注意
console.log(undefined instanceof undefined); // 报错
console.log(null instanceof null); // 报错

三、constructor

constructor 是每个实例对象都拥有的属性

constructor有两个作用:

  1. 判断数据的类型;
  2. 对象实例通过 constrcutor 对象访问它的构造函数;
function Hello() {}; // 构造函数
var h = new Hello(); // 实例化对象console.log(Hello.prototype.constructor == Hello); // true
console.log(h.constructor == Hello); // true ()console.log(("1").constructor === String); // true
console.log((1).constructor === Number); // true
console.log((NaN).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(([]).constructor === Array); // true
console.log((function () {}).constructor === Function); // true
console.log(({}).constructor === Object); // true
console.log((Symbol(1)).constructor === Symbol); // trueconsole.log((null).constructor === Null); // 报错
console.log((undefined).constructor === Undefined); // 报错

costructor来判断类型看起来是完美的,然而,如果我创建一个对象,更改它的原型,这种方式也变得不可靠了

function Fn(){};
Fn.prototype=new Array(); // 改变原型
var f=new Fn();console.log(f.constructor===Fn);    // false
console.log(f.constructor===Array); // true 

这里声明了一个Fn的构造函数,并且把他的原型指向了Array的原型,所以这种情况下,constructor也显得力不从心了。

四、Object.prototype.toString.call()

使用 Object 对象的原型方法 toString 来判断数据类型:完美精准 的返回各种数据类型

const a = Object.prototype.toString;console.log(a.call(1)); // [object Number]
console.log(a.call("1")); // [object String]
console.log(a.call(NaN)); // [object Number]
console.log(a.call(true)); // [object Boolean]
console.log(a.call(Symbol(1))); // [object Symbol]
console.log(a.call(null)); // [object Null]
console.log(a.call(undefined)); // [object Undefined]
console.log(a.call([])); // [object Array]
console.log(a.call({})); // [object Object]
console.log(a.call(function () {})); // [object Function]function Fn(){};
Fn.prototype=new Array(); // 改变原型
var f=new Fn();
console.log(a.call(Fn)); // [object Function]

稍微简单封装下:

// 定义检测数据类型的功能函数
function checkedType(target) {return Object.prototype.toString.call(target).slice(8, -1);
}console.log(checkedType(1)); // Number
console.log(checkedType("1")); // String
console.log(checkedType(NaN)); // Number
console.log(checkedType(true)); // Boolean
console.log(checkedType(Symbol(1))); // Symbol
console.log(checkedType(null)); // Null
console.log(checkedType(undefined)); // Undefined
console.log(checkedType([])); // Array
console.log(checkedType({})); // Object
console.log(checkedType(function () {})); // Function

Object.prototype.toString.call(obj)类型检测原理

Object原型上的toString方法作用在传入的obj的上下文中(通过callthis指向obj

五、__proto__

var arr = []'
arr.__proto__ === Array.prototype; // truevar obj = {}'
obj.__proto__ === Object.prototype; // truevar str = '';
str.__proto__ === String.prototype; // truevar num = 0;
num.__proto__ === Number.prototype; // true

六、 其他

  1. 通过ES6的Array.isArray()做判断

Array.isArrray(obj);
  1. 通过Array.prototype.isPrototypeOf

Array.prototype.isPrototypeOf(obj)

http://www.zhongyajixie.com/news/12447.html

相关文章:

  • 建设网站需申请什么资料怎么做
  • 学院网站建设新闻简报宁波seo网络推广渠道介绍
  • 天津做手机网站建设百度网址导航
  • 如何确定网站栏目外贸网站优化公司
  • js 做网站seo策略工具
  • 展厅设计素材网站我想在百度发布信息
  • 网站做专题提升权重深圳sem竞价托管
  • p2p网站方案描述建设一个网站的具体步骤
  • 生鲜配送网站建设长沙互联网推广公司
  • 佛山做外贸网站服务营销渠道策略有哪些
  • 济阳网站建设哪家好google chrome download
  • 网站建设的培训心得香港服务器
  • 网站程序定制开发流程谷歌seo详细教学
  • 瀑布流网站后台企业员工培训课程
  • 凡科 预约网站百度网盘搜索入口
  • 海拉尔网站开发搜狗收录批量查询
  • 服装网站建设推荐企业中层管理人员培训课程
  • 网站模板怎样发布怎么交换友情链接
  • 深圳做网站排名公司推荐深圳知名seo公司
  • wordpress制作企业网站杭州网站提升排名
  • 做的比较好的医院网站泉州排名推广
  • 企业融资方式天津搜索引擎优化
  • 通辽做网站的公司品牌策划的五个步骤
  • h5做网站教程游戏推广员是做什么的
  • 惠州seo快速排名重庆做网络优化公司电话
  • 苹果销售网站怎么做的爱站网排行榜
  • 商城网站大全武汉整站seo数据上云
  • 有哪些外国网站做精油的视频号下载器手机版
  • 建网站可以赚钱吗如何制作个人网站
  • 做设计拍摄的网站平台百度百度一下官网