当前位置:网站首页>Typeof and instanceof, how to simulate the implementation of an instanceof? Is there a general detection data type?
Typeof and instanceof, how to simulate the implementation of an instanceof? Is there a general detection data type?
2022-06-12 12:34:00 【Fashion_ Barry】
There is a good way to study , There's no end to learning how to make a boat !!! Kim San Yin four , Come on for the interview ; blunt !!!
One 、typeof
1. typeof advantage ? shortcoming ?
advantage : Be able to quickly distinguish basic data types
shortcoming : Can't be Object、Array and Null distinguish , All back to object
2. typeof effect ?
Differentiate data types , Can return 7 Type of data :number、string、boolean、undefined、object、function , as well as ES6 Newly added symbol
3. typeof Can you correctly distinguish data types ?
You can't . For primitive types , except null Can be judged correctly ; For reference types , except function Outside , Will return to "object"
4. typeof matters needing attention
typeofThe return value isstringFormat , Pay attention to questions like this :typeof(typeof(undefined)) -> "string"typeofUndefined variables will not report errors , return"undefiend"typeof(null) -> "object": Long leftbugtypeofCannot distinguish an array from a normal object :typeof([]) -> "object"typeof(NaN) -> "number"
5. typeof Why is it right null Display of errors
This is just JS There is a long history Bug. stay JS In the original version of 32 Bit system , For performance reasons, use low-level storage variables for type information ,000 The beginning represents the object null It's all zero , So it's wrong to say object
6. typeof('abc') and typeof 'abc' All are string, that typeof Operator or function ?
answer :typeof It's the operator
reason :
typeofOne of the return values of is'function', Iftypeofbyfunction, thattypeof(typeof)Returns the'function', But tested , The above code browser will throw an error . So it can be proved thattypeofNot a function .- since
typeofIt's not a function , thattypeofThe following parentheses are used to ?Parentheses are used for grouping rather than function calls .—— 《javascript Advanced programming 》
Two 、instanceof principle
advantage : Be able to distinguish Array、Object and Function, Suitable for judging custom class instance objects shortcoming :Number,Boolean,String Basic data type can't judge
① instanceof Determine whether the prototype of the constructor exists on the prototype chain of the object . Only reference type can be judged .
② instanceof Often used to judge A Is it B Example
// A yes B Example , return true, Otherwise return to false
// Judge A Whether or not there is B The prototype of the
A instaceof B
3、 ... and 、typeof And instanceof The difference between
typeof And instanceof Are methods for judging data types , The difference between the following :
- typeof Will return the basic type of a variable ,instanceof It returns a Boolean value
- instanceof You can accurately judge complex reference data types , However, the basic data type cannot be correctly judged
- and typeof There are also disadvantages , Although it can judge the basic data type (null With the exception of ), But in the reference data type , except function Out of type , Others cannot be judged
Four 、 The simulation implements a instanceof
thought : Look up the prototype chain
instance_of (Case, Constructor) {
console.log('Case:', typeof Case, 'Constructor1:', typeof Constructor);
// Basic data type return false
if ((typeof (Case) != 'object' && typeof (Case) != 'function') || Case == 'null') return false
let CaseProto = Object.getPrototypeOf(Case);
console.log('CaseProto:', CaseProto);
console.log('Constructor:', Constructor.prototype);
while (true) {
// Find the top of the prototype chain , Still not found , return false
if (CaseProto == null) return false;
// Same type found
if (CaseProto == Constructor.prototype) return true;
CaseProto = Object.getPrototypeOf(CaseProto)
}
}, function A () { }
function B () { }
const C = new A();
const D = new B();
console.log(this.instance_of(C, A)); // true
console.log(this.instance_of(D, B)); // true
console.log(this.instance_of(C, Array)); // false
5、 ... and 、 Common monitoring data types :Object.prototype.toString.call()
advantage : Accurate judgment of data type shortcoming : The writing method is tedious and not easy to remember , Recommended for use after encapsulation
Object.prototype.toString.call(()=>{}) // [object Function]
Object.prototype.toString.call({}) // [object Object]
Object.prototype.toString.call([]) // [object Array]
Object.prototype.toString.call('') // [object String]
Object.prototype.toString.call(22) // [object Number]
Object.prototype.toString.call(undefined) // [object undefined]
Object.prototype.toString.call(null) // [object null]
Object.prototype.toString.call(new Date) // [object Date]
Object.prototype.toString.call(Math) // [object Math]
Object.prototype.toString.call(window) // [object Window]边栏推荐
- Autolock solves the problem of forgetting to unlock after locking
- imx6-uboot添加lvds1显示
- Backtracking, eight queens
- ITK 多阶段配准
- Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220602 version deployed interface applicable to any development language
- 机械臂改进的DH参数与标准DH参数理论知识
- 关系代数笛卡尔积和自然连接的例子
- Lightweight ---project
- JS pre parsing, object, new keyword
- El select data echo, display only value but not label
猜你喜欢
随机推荐
从小白入手,从已经训练好的模型中取出weight权重参数绘制柱状图
2021-11-16
NewOJ Week 10题解
Point cloud registration -- GICP principle and its application in PCL
恭喜Splashtop 荣获2022年 IT Europa “年度垂直应用解决方案”奖
AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
Rust language learning
2021-11-16
Advanced chapter of C language -- ten thousand words explanation pointer and qsort function
Boot entry directory
Map and set of ES6
拿来就能用的网页动画特效,不来看看?
Autolock solves the problem of forgetting to unlock after locking
宏编译 预处理头 WIN32_LEAN_AND_MEAN
大学生请假理由
BAT面试&高级进阶,文末领取面试资料
鸡尾酒排序
JS attribute operation and node operation
golang的channel和条件变量在单生产单消费场景下的性能对比测试
The difference between bind, call and apply, and the encapsulation of bind()









