国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

JS中typeof與instanceof的區(qū)別

2019-11-14 11:57:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

javaScript 中 typeof 和 instanceof 常用來(lái)判斷一個(gè)變量是否為空,或者是什么類(lèi)型的。但它們之間還是有區(qū)別的:typeoftypeof 是一個(gè)一元運(yùn)算,放在一個(gè)運(yùn)算數(shù)之前,運(yùn)算數(shù)可以是任意類(lèi)型。它返回值是一個(gè)字符串,該字符串說(shuō)明運(yùn)算數(shù)的類(lèi)型。typeof 一般只能返回如下幾個(gè)結(jié)果:number,boolean,string,function,object,undefined。我們可以使用 typeof 來(lái)獲取一個(gè)變量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因?yàn)槿绻?a 不存在(未聲明)則會(huì)出錯(cuò),對(duì)于 Array,Null 等特殊對(duì)象使用 typeof 一律返回 object,這正是 typeof 的局限性。

網(wǎng)上的一個(gè)小例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script language="Javascript" type="text/javascript">document.write ("typeof(1): "+typeof(1)+"<br>");document.write ("typeof(NaN): "+typeof(NaN)+"<br>");document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>");document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>");document.write ("typeof(/"123/"): "+typeof("123")+"<br>");document.write ("typeof(true): "+typeof(true)+"<br>");document.write ("typeof(window): "+typeof(window)+"<br>");document.write ("typeof(Array()): "+typeof(new Array())+"<br>");document.write ("typeof(function(){}): "+typeof(function(){})+"<br>");document.write ("typeof(document): "+typeof(document)+"<br>");document.write ("typeof(null): "+typeof(null)+"<br>");document.write ("typeof(eval): "+typeof(eval)+"<br>");document.write ("typeof(Date): "+typeof(Date)+"<br>");document.write ("typeof(sss): "+typeof(sss)+"<br>");document.write ("typeof(undefined): "+typeof(undefined)+"<br>")</script><title>javascript類(lèi)型測(cè)試</title></head><body></body></html>instanceofinstance:實(shí)例,例子a instanceof b?alert("true"):alert("false"); //a是b的實(shí)例?真:假instanceof 用于判斷一個(gè)變量是否某個(gè)對(duì)象的實(shí)例,如 var a=new Array();alert(a instanceof Array); 會(huì)返回 true,同時(shí) alert(a instanceof Object) 也會(huì)返回 true;這是因?yàn)?Array 是 object 的子類(lèi)。再如:function test(){};var a=new test();alert(a instanceof test) 會(huì)返回談到 instanceof 我們要多插入一個(gè)問(wèn)題,就是 function 的 arguments,我們大家也許都認(rèn)為 arguments 是一個(gè) Array,但如果使用 instaceof 去測(cè)試會(huì)發(fā)現(xiàn) arguments 不是一個(gè) Array 對(duì)象,盡管看起來(lái)很像。另外:測(cè)試 var a=new Array();if (a instanceof Object) alert('Y');else alert('N');得'Y’但 if (window instanceof Object) alert('Y');else alert('N');得'N'。(PS:經(jīng)證實(shí),目前主流瀏覽器都返回Y。)所以,這里的 instanceof 測(cè)試的 object 是指 js 語(yǔ)法中的 object,不是指 dom 模型對(duì)象。使用 typeof 會(huì)有些區(qū)別alert(typeof(window)) 會(huì)得 object


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 内丘县| 库尔勒市| 江源县| 肇源县| 会理县| 乾安县| 新乐市| 卓尼县| 开封县| 平潭县| 城口县| 江油市| 西充县| 仙游县| 麻江县| 集贤县| 浏阳市| 建水县| 德令哈市| 青龙| 衢州市| 南昌市| 桐乡市| 永城市| 永昌县| 水城县| 沙坪坝区| 洛扎县| 泰宁县| 兴文县| 名山县| 宁南县| 武山县| 厦门市| 靖西县| 万盛区| 柏乡县| 宁蒗| 金川县| 合江县| 旌德县|