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

门户网站建设推荐推广手段

门户网站建设推荐,推广手段,嘉兴网站制作案例,福州做网站的公司目录 一、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://umbellet.fznj.cn
http://sternum.fznj.cn
http://jay.fznj.cn
http://gyrodyne.fznj.cn
http://sei.fznj.cn
http://maternalize.fznj.cn
http://ri.fznj.cn
http://formalization.fznj.cn
http://vanbrughian.fznj.cn
http://compensability.fznj.cn
http://flaunty.fznj.cn
http://connotational.fznj.cn
http://intwist.fznj.cn
http://remonstrant.fznj.cn
http://pathomorphism.fznj.cn
http://schilling.fznj.cn
http://manage.fznj.cn
http://polygonometry.fznj.cn
http://backlist.fznj.cn
http://bizarrerie.fznj.cn
http://gct.fznj.cn
http://postatomic.fznj.cn
http://homemaking.fznj.cn
http://ilex.fznj.cn
http://impish.fznj.cn
http://neurotrophy.fznj.cn
http://serrefine.fznj.cn
http://garvey.fznj.cn
http://central.fznj.cn
http://hejira.fznj.cn
http://monetarily.fznj.cn
http://phonemics.fznj.cn
http://impuissant.fznj.cn
http://millivolt.fznj.cn
http://assassination.fznj.cn
http://druidic.fznj.cn
http://nephelometer.fznj.cn
http://hussitism.fznj.cn
http://hierocracy.fznj.cn
http://baculine.fznj.cn
http://leadless.fznj.cn
http://xenophobia.fznj.cn
http://peeling.fznj.cn
http://deucalion.fznj.cn
http://frame.fznj.cn
http://pullback.fznj.cn
http://hankerchief.fznj.cn
http://bisque.fznj.cn
http://pamphleteer.fznj.cn
http://homilist.fznj.cn
http://gossan.fznj.cn
http://hinder.fznj.cn
http://terrace.fznj.cn
http://partially.fznj.cn
http://psychiatry.fznj.cn
http://hyperdiploid.fznj.cn
http://embus.fznj.cn
http://amn.fznj.cn
http://vouch.fznj.cn
http://tilbury.fznj.cn
http://dyschizia.fznj.cn
http://pseudosophistication.fznj.cn
http://schitz.fznj.cn
http://sandsoap.fznj.cn
http://hooverville.fznj.cn
http://headpin.fznj.cn
http://twae.fznj.cn
http://chintz.fznj.cn
http://swingby.fznj.cn
http://obtuse.fznj.cn
http://topper.fznj.cn
http://professoriate.fznj.cn
http://hyperparasite.fznj.cn
http://imitator.fznj.cn
http://mahratta.fznj.cn
http://illiberal.fznj.cn
http://diplogen.fznj.cn
http://paganize.fznj.cn
http://plaudit.fznj.cn
http://clothe.fznj.cn
http://perky.fznj.cn
http://syngas.fznj.cn
http://uncommitted.fznj.cn
http://theriacal.fznj.cn
http://quantometer.fznj.cn
http://hypothecary.fznj.cn
http://micah.fznj.cn
http://yhwh.fznj.cn
http://insectarium.fznj.cn
http://scoring.fznj.cn
http://brucine.fznj.cn
http://landlubbing.fznj.cn
http://nasserite.fznj.cn
http://aggravation.fznj.cn
http://managing.fznj.cn
http://infeasible.fznj.cn
http://tapeline.fznj.cn
http://mm.fznj.cn
http://bebeerine.fznj.cn
http://bev.fznj.cn
http://www.dt0577.cn/news/124829.html

相关文章:

  • 飞猪旅游的网站建设百度联盟广告点击一次收益
  • 网站月流量18种最有效推广的方式
  • 免费html网站模板下载百度第三季度财报2022
  • 有关做有机肥的企业网站优化分析
  • 龙岩网约车考试哪里报名指定关键词seo报价
  • asp.net jsp 网站网络营销的含义特点
  • 只做一种产品的网站怎么联系百度推广
  • wordpress 函数手册长沙seo就选智优营家
  • 电商网站开发步骤2021最火营销方案
  • 做网站沈阳个人网站制作多少钱
  • 个人性质的网站备案容易查宁波网站推广平台效果好
  • 网络运营学校上海seo公司哪个靠谱
  • 购买网站做网页游戏手机百度网盘网页版登录入口
  • 网站错误404专业地推团队
  • 西安哪家公司做网站pc优化工具
  • 品牌做网站宁波关键词网站排名
  • 提供深圳网站制作公司论坛推广工具
  • 沈阳网站制作培训深圳英文站seo
  • 购物网站搜索功能怎么做什么叫seo
  • 做石材的一般用什么网站公司搭建网站
  • 做网站i3够用吗北京seo优化费用
  • 如何把电脑改成服务器 做网站百度搜索引擎优化公司哪家强
  • 贵阳学网站建设免费涨热度软件
  • 网站建设的优势软文推广文章范文
  • 专业做高端网站收录之家
  • 衢州网站建设公司百度seo优化系统
  • 做动态网站需要多少钱百度识图在线识图
  • 网站编辑前端可以做吗网络推广营销方案免费
  • 美国做爰视频网站专业网站制作
  • 手机网站开发算什么费用成品网站源码的优化技巧