前言:js面试题,先更新点简单的。(持续更新js面试题)
-
js的数据类型(难度:★)
答:基本数据类型:
Number类型,String类型,Booolean类型,Null类型,Undefined类型,复杂数据类型:
Array类型,Object类型,Function类型 -
如何判断变量的数据类型,有几种方法(难度:★)
答:
-
typeof<script> var a = "iamstring."; var b = 222; var c = [1, 2, 3]; var d = new Date(); var e = function () { alert(111); }; var f = function () { this.name = "22"; }; console.log(typeof a);//string console.log(typeof b);//number console.log(typeof c);//object console.log(typeof d);//object console.log(typeof e);//function console.log(typeof f);//function </script> -
instanceof判断构造函数的prototype是否出现再实例对象的原型链上。console.log(a instanceof Object);//false console.log(b instanceof Number);//false console.log(c instanceof Array);//true console.log(d instanceof Date);//true console.log(e instanceof Function);//true console.log(f instanceof Function);//true -
constructor返回创建实例对象的 Object 构造函数的引用。(对象.constructor)console.log(a.constructor === String)// true console.log(b.constructor === Number)// true console.log(c.constructor === Array)// true console.log(d.constructor === Date)// true console.log(e.constructor === Function)//true -
Object.prototype.toString.call()===’[object Array]’
console.log(Object.prototype.toString.call(a) === '[object String]')// true; console.log(Object.prototype.toString.call(b) === '[object Number]') // true; console.log(Object.prototype.toString.call(c) === '[object Array]')// true; console.log(Object.prototype.toString.call(d) === '[object Date]') //true; console.log(Object.prototype.toString.call(e) === '[object Function]') //true; console.log(Object.prototype.toString.call(f) === '[object Function]')//true; -
jQuery.type().无敌万能的方法。
-
如果对象是undefined或null,则返回undefined或null。
-
如果对象有一个内部的对象和一个浏览器的内置对象,我们返回相应的对象名称。
jQuery.type(undefined) === "undefined" jQuery.type() === "undefined" jQuery.type(window.notDefined) === "undefined" jQuery.type(null) === "null" jQuery.type(true) === "boolean" jQuery.type(3) === "number" jQuery.type("test") === "string" jQuery.type(function () { }) === "function" jQuery.type([]) === "array" jQuery.type(new Date()) === "date" jQuery.type(new Error()) === "error" jQuery.type(/test/) === "regexp"
-
-

被折叠的 条评论
为什么被折叠?



