//instanceof 判断引用类型,typeof判断基本类型。
function init() {
//testTypeof();
testInstanceof();
}
window.onload = init;
function testTypeof() {
var str = "123";
var num = 123;
var bool = false;
var unde_d = undefined;
var unde_d2;
var nul = null;
alert(typeof str == "string");//true 注意大小写,js区分大小写
alert(typeof str == "String");//false
//alert(typeof str == string); //错误写法 ,typeof 判断类型需用引号
alert(typeof num == "number");//true
alert(typeof bool == "boolean");//true
alert(typeof unde_d == "undefined");//true
alert(typeof unde_d2 == "undefined");//true
alert(typeof nul == "object"); //true
}
function testInstanceof() {
var str = "123";
var obj = new Object();
var array1 = new Array();
var func = function() {
this.name = "marry";
this.say = function() {
alert("hello " + this.name);
}
}
var func1 = new func();
alert(str instanceof String); //flase
alert(obj instanceof Object);//true
alert(array1 instanceof Array);//true
alert(func instanceof Object);//true
alert(typeof func);//function
alert(func1 instanceof Object);//true
alert(func1 instanceof func);//true
alert(typeof func1);//object
}
分享到:
相关推荐
typeof是个操作符,可以判断基本数据类型(返回的结果只能是number,string,boolean,null,symbol,function,object) 返回值分以下几种 对于基本类型。除了null值返回object以外,其他均返回正确的结果 对于引用...
typeof用来检测给定变量的数据类型(也可叫做基本类型,基本数据类型。包含undefined、boolean、string、number、object、function)var message = “so easy”;alert(typeof message); //”string”alert(...
数据类型判断的方法 在探索数据类型判断方法的时候我们...我相信typeof这个判断数据类型的方法是大家平常用的比较多的,闲话不多说,直接上代码: console.log(typeof 1);//number console.log(typeof ‘hello’);//st
一般用于判断基本数据类型,用于判断引用数据类型和null时会发生意外的错误 typeof 1 // number typeof '1' // string typeof true // boolean typeof Symbol('1') // symbol typeof undefined // undefined typeof...
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。ECMAScript 引入了另一个 Java 运算符...
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。ECMAScript 引入了另一个 Java 运算符...
2. 判断数据类型typeof、instanceof、Object.prototype.toString.call()、constructor 2.1 判断数组的几种方式 2.2 判断NaN的几种方式 2.3 实现一个函数clone 3. 类数组与数组的区别与转换 4. 数组的常见API 5. bind...
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。这就需要用到instanceof来检测某个...
15.基本数据类型和引用类型在存储上的差别 16.浏览器Eventloop和Node中有什么区别 17.setTimeout倒计时误差 18.数组降维 19.深拷贝 20.typeof与instanceof区别 21.cookie、localstorage、session、indexDB区别 22....
面试官问:typeof 可以判断哪些类型?instanceof 做了什么?null为什么被typeof错误的判断为object JavaScript进阶-执行上下文(理解执行上下文一篇就够了) JavaScript进阶-执行上下文栈和变量对象(一周一更) 再来40...
要区分对象的具体类型,用instanceof运算符。 7) 在函数体内有隐含的arguments对象,可取得所有传入的参数,可用类似数组的方式读/写各参数,length属性表示参数的个数。但函数的只读属性length表示函数实际定义的...