一.JavaScript判断对象类型 1.可以使用typeof函数判断对象类型 function checkObject1(){ var str="str"; console.log(typeof(str))//输出"string"; console.log(typeof(str)=="string")//输出true; } 2.使用对象的构造函数属性(constructor),来判断对象的类型: function checkObject2()
学习内容: 一.使用instanceof操作符判断对象类型 1.instanceof操作符可以判断一个实例对象是否属于一个类. 语法:对象名 instanceof 类名 2.使用instanceof表达式的返回值为布尔值,如果返回值为true,说明此对象是该类的实例对象,如果返回值为false,说明此对象不是该类的实例对象. 示例代码: class Grandfather{//父类 public static void draw(Grandfather p) { }}class Father e
对于确定JS内置对象类型,JS提供了typeof运算符,该运算符得到的结果为以下6种:number,boolean,string,function,object,undefined.不过对绝大多数对象而言,typeof都返回"object",无法确定具体的类型.我们使用一种函数Object.prototype.toString.call来判断 <script> var a = 1; console.log("a:"+typeof a); //number