javascript中typeof操作符和constucor属性检测
作者:hebedich 发布时间:2024-05-09 10:37:27
标签:javascript,typeof,操作符,constucor
*#type.js
function Person(name, age) {
this.name = name;
this.age = age;
}
var d = {an: 'object'};
var a = ['apple', 'banana'];
var f = function() {};
var s = 'David';
var n = 33;
var b = true;
var o = new Object();
var person = new Person('Mark', 22);
console.log(typeof(d) + ': ' + d.constructor);
console.log(typeof(a) + ': ' + a.constructor);
console.log(typeof(f) + ': ' + f.constructor);
console.log(typeof(s) + ': ' + s.constructor);
console.log(typeof(n) + ': ' + n.constructor);
console.log(typeof(b) + ': ' + b.constructor);
console.log(typeof(o) + ': ' + o.constructor);
console.log(typeof(person) + ': ' + person.constructor);
运行$node type.js得
object: function Object() { [native code] }
object: function Array() { [native code] }
function: function Function() { [native code] }
string: function String() { [native code] }
number: function Number() { [native code] }
boolean: function Boolean() { [native code] }
object: function Object() { [native code] }
object: function Person() { [native code] }
可见, 使用typeof操作符和constucor属性检测对象类型返回值是存在差异的.
如果变量是数组, typeof操作符返回object, constructor属性返回Array;
如果变量是构造函数对象, typeof操作符返回object, constructor属性返回该构造函数
每个变量都有其construcor属性, 这个属性不单单提供了这个是否对象, 还提供了这个对象是什么类型的对象. 总之, constructor属性保存了一个指向对象的构造函数, 无论它是自定义的还是原生类型的对象.
有一点需要注意的是, 不同的浏览器对typeof操作符检测正则表达式会有所不同,IE和Firefox会返回'object'.
好了,今天内容就先到这里了,小伙伴们如有疑问,就在下方留言吧。
0
投稿
猜你喜欢
- 本文主要是利用scapy包编写了一个简易扫描工具,支持ARP、ICMP、TCP、UDP发现扫描,支持TCP SYN、UDP端口扫描,如下:u
- 什么是模板匹配模板匹配是指在当前图像A内寻找与图像B最相似的部分,可以理解找茬,但是这里是找出一样的信息。一般我们将图像A称为输入图像,将图
- 这篇文章主要介绍了使用python远程操作linux过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 一.random模块 随机 random() &
- 在Matplotlib实际使用中会有生成不同大小subplots的需求。import numpy as np import matplotl
- 在这篇文章中,我们将讨论mask R-CNN背后的一些理论,以及如何在PyTorch中使用预训练的mask R-CNN模型。1.语义分割、目
- 任务:基于线程池来操作MySQL,测试单台机器读写MySQL单表的效率。思路:创建一个大小合适的线程池,让每个线程分别连接到数据库并进行读取
- 在本文中,我们向您介绍一些提示和技巧,以帮助您更快地编写代码Python的可读性和设计简单性是其广受欢迎的两个主要原因。一些常见的Pytho
- 本文实例讲述了Python实现给文件添加内容及得到文件信息的方法。分享给大家供大家参考。具体分析如下:经常会遇到给文件添加内容的时候,如果只
- 本文实例为大家分享了python实现移位加密和解密的具体代码,供大家参考,具体内容如下代码很简单,就不多做解释啦。主要思路是将字符串转为As
- 如果不配置.gitignore的文件,带push代码的时候就会把一写不必要的文件push到远程仓库,如.idea文件。如果不小心出现此文件在
- 张同学 10.4号开始发视频,视频的点赞量一直很高,11.17 号的视频达到了顶峰,收获 250w 个赞,之后关注量也开启了暴涨。所以挖掘
- 如何利用网页弹出各种形式的窗口,我想大家大多都是知道些的,但那种多种多样的弹出式窗口是怎么搞出来的,我们今天就来学习一下:推荐:网页弹出窗口
- 1.问题描述请编写程序,实现以下功能:在字符串中的所有数字字符前加一个“$”符号。例如,输入A1B2
- 函数内省(function introspection)除了__doc__属性, 函数对象还有很多属性,对于下面的函数,可以使用dir()查
- python实现简单的情感分析1 数据导入及预处理1.1 数据导入# 数据导入import pandas as pddata =
- 01. 应用场景及定义方式应用场景在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到私有属性
- 前言最近在使用pytorch的时候,模型训练时,不需要使用forward,只要在实例化一个对象中传入对应的参数就可以自动调用 forward
- 本文实例讲述了Python基于递归实现电话号码映射功能。分享给大家供大家参考,具体如下:问题电话按键上面的每个数字都对应着几个字母,如果按下
- <html> <head> <title>JavaScript Data Access Test<