
如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
如何判断js中的类型呢,先举几个例子:
var a = "iamstring";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e =
function(){alert(111);};
var f =
function(){thisname="22";};
最常见的判断方法:typeof
alert(typeof a)
------------> string
alert(typeof b)
------------> number
alert(typeof c)
------------> object
alert(typeof d)
------------> object
alert(typeof e)
------------> function
alert(typeof f)
------------> function
其中typeof返回的类型都是字符串形式,需注意,例如:
alert(typeof a == "string")
-------------> true
alert(typeof a == String)
---------------> false
另外typeof
可以判断function的类型;在判断除Object类型的对象时比较方便。
判断已知对象类型的方法: instanceof
alert(c instanceof Array)
---------------> true
alert(d instanceof
Date)
alert(f instanceof Function)
------------> true
alert(f instanceof function)
------------> false
注意:instanceof
后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
根据对象的constructor判断:
constructor
alert(cconstructor ===
Array) ----------> true
alert(dconstructor === Date)
-----------> true
alert(econstructor ===
Function) -------> true
注意: constructor 在类继承时会出错
eg,
function A(){};
function B(){};
Aprototype = new B(); //A继承自B
var aObj = new A();
alert(aobjconstructor === B) ----------->
true;
alert(aobjconstructor === A) ----------->
false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
alert(aobj instanceof B) ---------------->
true;
alert(aobj instanceof B) ---------------->
true;
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
aobjconstructor = A;
//将自己的类赋值给对象的constructor属性
alert(aobjconstructor === A) ----------->
true;
alert(aobjconstructor === B) ----------->
false; //基类不会报true了;
通用但很繁琐的方法: prototype
alert(ObjectprototypetoStringcall(a) === ‘[object String]’)
-------> true;
alert(ObjectprototypetoStringcall(b) === ‘[object Number]’)
-------> true;
alert(ObjectprototypetoStringcall(c) === ‘[object Array]’)
-------> true;
alert(ObjectprototypetoStringcall(d) === ‘[object Date]’)
-------> true;
alert(ObjectprototypetoStringcall(e) === ‘[object Function]’)
-------> true;
alert(ObjectprototypetoStringcall(f) === ‘[object Function]’)
-------> true;
大小写不能写错,比较麻烦,但胜在通用。
通常情况下用typeof
本篇文章主要是对js
substring从右边获取指定长度字符串的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
如下所示:
代码如下:
/
Get
the
rightmost
substring,
of
the
specified
length,
from
a
String
object
/
Stringprototyperight
=
function
(length_)
{
var
_from
=
thislength
-
length_;
if
(_from
<
0)
_from
=
0;
return
thissubstring(thislength
-
length_,
thislength);
};
少年,
1、jQuery获取到的这个form当然是一个jQuery 对象;
2、你要是想alert()输出的是字符串,你要想查看对象的内容就得在控制台里面看(consolelog(obj));
3、应该是form[0]elements,而不是formelements。
以下代码在“W3C在线测试工具 V2”中通过测试:
><html>
<head>
<script type="text/javascript" src="/jquery/jqueryjs"></script>
<style>
box{
display:none;
}
</style>
</head>
<body>
<form id="reg" method="get" action="ajax-testphp">
<p>账号:<input type="text" id="username" name="user">
<div class="box">信息框</div>
</p>
<p>密码:<input type="text" id="pass" name="psd"></p>
<p><input type="button" id="sub" name="sub" value="提交"/></p>
</form>
<script>
$(function(){
$('#sub')click(function(){
serialize($("#reg")first());
});
});
function serialize(form){
consolelog(form);
var parts=new Array();
alert(form);
alert(form[0]elements);
}
</script>
</body>
</html>
expand ->0, then you can get "elements"
以上就是关于如何获取JS变量类型全部的内容,包括:如何获取JS变量类型、js substring从右边获取指定长度字符串、跪求各位js高手,为什么alert(form)是一个对象,alert(form.elements)显示undefined等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)