Comments (1)
- Object.prototype.toString.call()
每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型。但当除了 Object 类型的对象外,其他类型直接使用 toString 方法时,会直接返回都是内容的字符串,所以我们需要使用call或者apply方法来改变toString方法的执行上下文。
const an = ['Hello','An'];
an.toString(); // "Hello,An"
Object.prototype.toString.call(an); // "[object Array]"
这种方法对于所有基本的数据类型都能进行判断,即使是 null 和 undefined 。
Object.prototype.toString.call('An') // "[object String]"
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call(Symbol(1)) // "[object Symbol]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call(function(){}) // "[object Function]"
Object.prototype.toString.call({name: 'An'}) // "[object Object]"
Object.prototype.toString.call() 常用于判断浏览器内置对象时。
更多实现可见 谈谈 Object.prototype.toString
- instanceof
instanceof 的内部机制是通过判断对象的原型链中是不是能找到类型的 prototype。
使用 instanceof判断一个对象是否为数组,instanceof 会判断这个对象的原型链上是否会找到对应的 Array 的原型,找到返回 true,否则返回 false。
[] instanceof Array; // true
但 instanceof 只能用来判断对象类型,原始类型不可以。并且所有对象类型 instanceof Object 都是 true。
[] instanceof Object; // true
- Array.isArray()
功能:用来判断对象是否为数组
instanceof 与 isArray
当检测Array实例时,Array.isArray 优于 instanceof ,因为 Array.isArray 可以检测出 iframes
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]
// Correctly checking for Array
Array.isArray(arr); // true
Object.prototype.toString.call(arr); // true
// Considered harmful, because doesn't work though iframes
arr instanceof Array; // false
Array.isArray() 与 Object.prototype.toString.call()
Array.isArray()是ES5新增的方法,当不存在 Array.isArray() ,可以用 Object.prototype.toString.call() 实现。
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
from interview.
Related Issues (20)
- Day388:实现一个sum函数,接收一个arr,累加arr的项,只能使用add方法,该方法接收两个数,模拟异步请求后端返回一个相加后的值?
- Day389:实现二进制与十进制的互相转化的两个函数? HOT 1
- Day390:下面的代码有什么问题?怎么优化? HOT 1
- Day391:css 的渲染层合成是什么? 浏览器如何创建新的渲染层?
- Day392:webpack中的hash、chunkhash、contenthash区别?(百度)
- Day393:请实现 find 函数,使下列的代码调用正确.(蚂蚁金服) HOT 3
- Day394:SSR 怎么做的,怎么保证同构?server 端的数据都 renderToString 了,为什么还要给到 client 端?
- Day395:vue-router 中路由方法 pushState 和 replaceState 能否触发 popSate 事件?
- Day396:为什么 vite、snowpack 可以比 webpack 快那么多?具体原理是什么?(阿里)
- Day397:手写 Vue.extend 实现?
- Day398: 怎么在指定数据源里面生成一个长度为 n 的不重复随机数组 能有几种方法 时间复杂度多少(字节)
- Day399: 如何找到数组中第一个没出现的最小正整数 怎么优化(字节)
- Day400:判断括号字符串是否有效(小米) HOT 1
- Day401:实现一个数字加减法功能(腾讯) HOT 1
- Day402: 实现一个render(str,parameter)方法,将str中的占位符用parameter填充?(腾讯) HOT 1
- Day403: 有一个二叉树,每个节点的值是一个正整数。写一个函数判断这棵树中是否存在从根到叶子节点的一个路径,这个路径上所有的节点之和为某个节点的值?
- Day404: 实现一个获取对象任意属性的方法?(字节) HOT 1
- Day405: 顺时针打印矩阵(美团)
- Day406: 写个函数,判断一个字符串是否为手机靓号?(腾讯)
- Day407: 有一堆整数,请把他们分成三份,确保每一份和尽量相等(11,42,23,4,5,6 4 5 6 11 23 42 56 78 90) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview.