typeof typeof是一个一元运算,放在一个运算数之前,运算数可以是任意类型。 它返回的值是一个字符串,该字符串说明运算数的类型。 typeof一般只能返回如下几个结果:number、boolean、string、function、object、undefined 我们可以使用typeof来获取一个变量是否存在,如
if(typeof a!='undefined'){
alert('ok')
}
而不要去使用if(a),因为如果a不存在(为声明)则会出错,对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。
instanceof instance:实例、例子
a instanceof b? alert('true'):alert('false'); //a是b的实例?真:假
instanceof用于判断一个变量是否是某个对象的实例,如
var a = new Array();
alert(a instanceof Array); //返回true
同时alert(a instanceof Object) 也会返回true,因为Array是object的子类。
constructor constructor属性返回对创建此对象的数组函数的引用; 例: 在本例中,我们将展示如何使用 constructor 属性:
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
document.write(bill.constructor);
</script>
输出:
function employee(name, job, born)
{this.name = name; this.job = job; this.born = born;}