constructor 属性返回变量或对象的构造函数。判断是否为日期,数组的例子
constructor 属性返回变量或对象的构造函数。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>constructor 属性返回变量或对象的构造函数。</title>
</head>
<body> <p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"john".constructor + "<br>" +
(3.14).constructor + "<br>" +
false.constructor + "<br>" +
[1,2,3,4].constructor + "<br>" +
{name:'john', age:34}.constructor + "<br>" +
new Date().constructor + "<br>" +
function () {}.constructor;
</script> </body>
</html>
运行结果:
function String() { [native code] }
function Number() { [native code] }
function Boolean() { [native code] }
function Array() { [native code] }
function Object() { [native code] }
function Date() { [native code] }
function Function() { [native code] }
判断是否为日期,数组的例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>判断是否为日期、数组</title>
</head>
<body>
<p>判断是否为日期。</p>
<p id="demo1"></p>
<p>判断是否为数组。</p>
<p id="demo2"></p>
<script>
var myDate = new Date();
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = isDate(myDate);
document.getElementById("demo2").innerHTML = isArray(fruits);
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
</script>
</body>
</html>
测试结果
判断是否为日期。 true 判断是否为数组。 true
constructor 属性返回变量或对象的构造函数。判断是否为日期,数组的例子的更多相关文章
- constructor 属性,判断是否为数组
<!--你可以使用 constructor 属性来查看是对象是否为数组 (包含字符串 "Array"):--><p>constructor属性返回变量或对象 ...
- 怎样理解prototype对象的constructor属性
function Person(name){ this.name = name; } var lilei = new Person("Lilei"); lilei.construc ...
- JavaScript constructor 属性详解
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数. 在JavaScript中,每个具有原型的对象都会自动获得constructor属性.除了arguments.Enu ...
- JS中constructor属性
constructor属性用于对当前对象的构造函数的引用.可以用来判断对象的类型: <script> var newStr = new String("One world One ...
- JavaScript 构造函数 prototype属性和_proto_和原型链 constructor属性 apply(),call()和bind() 关键字this
1.构造函数: 通常构造函数首字母需要大写,主要是为了区别ECMAScript的其它函数.(高程三 P145) 构造函数与其他函数的唯一区别,就在于调用它们的方式不同.只要通过new来调用,任何函数都 ...
- JavaScript对象中的constructor属性
constructor属性始终指向创建当前对象的构造函数. 比如下面的例子: // 等价于 var foo = new Array(1, 56, 34, 12); var arr = [1, 56, ...
- 检测对象类型的两种方式,constructor属性和instanceof
//本例是为了记录检测对象类型的两种方式,即constructor属性和instanceof操作符.详见<高三>P145 function Person(name, age, ...
- JavaScript类型检测, typeof操作符与constructor属性的异同
*#type.js function Person(name, age) { this.name = name; this.age = age; } var d = {an: 'object'}; v ...
- javascript的constructor属性
/* constructor 属性 constructor 属性返回所有 JavaScript 变量的构造函数. */console.log("John".constructor) ...
随机推荐
- 在使用html5的video标签播放视频时为何只有声音却没有图像
在使用html5的video标签播放视频时为何只有声音却没有图像? 答:使用格式化工厂转个编码就行了,MP4有3种编码,mpg4(xdiv),,mpg4(xvid),avc(h264)转换成H264编 ...
- 2018.09.14 洛谷P3931 SAC E#1 - 一道难题 Tree(树形dp)
传送门 简单dp题. f[i]表示以i为根的子树被割掉的最小值. 那么有: f[i]=min(∑vf[v],dist(i,fa))" role="presentation" ...
- hdu-1069(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:一群猴子,给出n块砖的长x宽y高z,用这些砖拼起的高度最高是多少, 要求底下的砖的长宽都要 ...
- gj5 自定义序列类
5.1 序列类型的分类 容器序列 list.tuple.deque扁平序列[同一种数据类型] str.bytes.bytearray.array.array可变序列 list, deque,by ...
- Deployment is out of date due to changes in the underlying project contents. Deployment is out of date due to changes in the underlying project contents. You'll need to manually 'Redeploy' the projec
原因1:导入的jar包路径不对,造成第一个错误, 原因2:设置右键工程->属性->myeclipse->web->deployment选use workbenk defaul ...
- I/O Planning
同一个BANK的电压必须是一样的,而电气特性则可以不同. 最近GTX调不出来,原来是电平不对 电源的影响 示波器看电源纹波 VCCO是为BANK的IO输出供电.比如LVCMOS33的信号,这个BANK ...
- hdu 5036 概率+bitset
http://acm.hdu.edu.cn/showproblem.php?pid=5036 n个房间每个房间里面有一把或多把钥匙可以打开其他的门.如果手上没有钥匙可以选择等概率随机选择一个门炸开,求 ...
- hdu 5023 线段树+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...
- onclick传参
var tema="<a title='打开' href='javascript:;' onclick='showKnowledgeMap(1,\" "+kl_na ...
- 集合(四)HashMap
之前的List,讲了ArrayList.LinkedList,最后讲到了CopyOnWriteArrayList,就前两者而言,反映的是两种思想: (1)ArrayList以数组形式实现,顺序插入.查 ...