chenhuiYj / chenhuiYj/note

js 数据检测

Open
#38 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

1.typeof

只能识别基础类型和引用类型,注意:null、 array、 DOM的判断都显示object

console.log(typeof 123); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof NaN); // "number"
console.log(typeof document.all); // "undefined"
console.log(typeof document.body); // "object"

2.constructor

指向创建该实例对象的构造函数,但 null 和 undefined 没有 constructor,以及 constructor 可以被改写,不太可靠

const arr = [1, 2, 3];
console.log(arr.constructor === Array) // true

const obj = {name: "守候", age: 22};
console.log(obj.constructor === Object) // true


String.prototype.constructor = function fn() {
  return {};
}

// constructor 可以被改写

let Parent =function(){}
let Sou=new Parent()
Sou.constructor===Object //false
Sou.constructor===Parent //true

3.instanceof

object instanceof Type :判断 object 是不是 Type 的实例(检测构造函数的原型Type.prototype是否存在于实例对象object的原型链上)。但只可用来判断引用数据,number,string,boolean等基本类型不是构造函数实例,不能用instanceof 判断。

let dataNumber=123
dataNumber instanceof Number //false

let dataStr=''
dataStr instanceof String //false

let dataArr=[]
dataArr instanceof Array //true

let dataObj={}
dataObj instanceof Object //true

4.Object.prototype.toString.call()

根据Object.prototype.toString() 方法得到对象内部属性 [[Class]],通过call或者apply改变this的指向,使this指向传进来的值,返回传进来的值的内部属性[[Class]]

Object.prototype.toString.call({})              // '[object Object]'
Object.prototype.toString.call([])              // '[object Array]'
Object.prototype.toString.call(() => {})        // '[object Function]'
Object.prototype.toString.call(‘守候’)        // '[object String]'
Object.prototype.toString.call(1)               // '[object Number]'
Object.prototype.toString.call(NaN)             // '[object Number]', NaN也是一种数字
Object.prototype.toString.call(true)            // '[object Boolean]'
Object.prototype.toString.call(Symbol())        // '[object Symbol]'
Object.prototype.toString.call(null)            // '[object Null]'
Object.prototype.toString.call(undefined)       // '[object Undefined]'
Object.prototype.toString.call(document.body)   //[object HTMLBodyElement]
Object.prototype.toString.call(document.all)     //[object HTMLAllCollection]
Object.prototype.toString.call(new Date())      // '[object Date]'
Object.prototype.toString.call(Math)            // '[object Math]'
Object.prototype.toString.call(window)          //[object Window]
Object.prototype.toString.call(new Set())       // '[object Set]'
Object.prototype.toString.call(new WeakSet())   // '[object WeakSet]'
Object.prototype.toString.call(new Map())       // '[object Map]'
Object.prototype.toString.call(new WeakMap())   // '[object WeakMap]'
function fn() {
    console.log(Object.prototype.toString.call(arguments))   //[object Arguments]
}

必须要Object.prototype.toString,不能是其他的.prototype.toString,因为只有Object.prototype.toString会返回数据类型

Function.prototype.toString.call(function(){return 1})   //'function(){return 1}'

Number.prototype.toString.call([])  //VM772:1 Uncaught TypeError: Number.prototype.toString requires that 'this' be a Number

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue contains Chinese documentation about JavaScript type detection, but names no file, test, or entry point. First identify where repository notes are stored and whether this content is intended for publication; done means adding or revising the relevant documentation with the requested scope confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.