JS 에서 type: of 와 instanceof 의 차이
7498 단어 jstypeof구별instanceof
typeof
type: of 는 1 원 연산 입 니 다. 하나의 연산 수 앞 에 놓 으 면 연산 수 는 임의의 유형 일 수 있 습 니 다.
반환 값 은 연산 수의 종 류 를 설명 하 는 문자열 입 니 다.type: of 는 일반적으로 다음 과 같은 몇 가지 결과 만 되 돌려 줍 니 다.
number,boolean,string,function,object,undefined。if (typeof a! = "undefined") {alert ("ok")} 와 같은 변 수 를 type of of of 를 사용 하여 가 져 올 수 있 습 니 다. if (a) 를 사용 하지 마 십시오. a 가 존재 하지 않 으 면 오류 가 발생 할 수 있 기 때 문 입 니 다. Array, Null 등 특수 대상 에 대해 type of 를 사용 하여 일률적으로 object 로 돌아 가 는 것 이 type of 의 한계 입 니 다.
인터넷 의 작은 예:
1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2: <html xmlns="http://www.w3.org/1999/xhtml">
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5: <script language="javascript" type="text/javascript">
6: document.write ("typeof(1): "+typeof(1)+"<br>");
7: document.write ("typeof(NaN): "+typeof(NaN)+"<br>");
8: document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>");
9: document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>");
10: document.write ("typeof(\"123\"): "+typeof("123")+"<br>");
11: document.write ("typeof(true): "+typeof(true)+"<br>");
12: document.write ("typeof(window): "+typeof(window)+"<br>");
13: document.write ("typeof(Array()): "+typeof(new Array())+"<br>");
14: document.write ("typeof(function(){}): "+typeof(function(){})+"<br>");
15: document.write ("typeof(document): "+typeof(document)+"<br>");
16: document.write ("typeof(null): "+typeof(null)+"<br>");
17: document.write ("typeof(eval): "+typeof(eval)+"<br>");
18: document.write ("typeof(Date): "+typeof(Date)+"<br>");
19: document.write ("typeof(sss): "+typeof(sss)+"<br>");
20: document.write ("typeof(undefined): "+typeof(undefined)+"<br>")
21: </script>
22: <title>javascript </title>
23: </head>
24:
25: <body>
26: </body>
27: </html>
인 스 턴 스: 인 스 턴 스, 예
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 의 하위 클래스 이기 때 문 입 니 다.또한: function test () {};var a=new test();alert (a instanceof test) 가 돌아 갑 니 다.
인 스 턴 트 of 에 대해 서 는 하나의 문 제 를 더 삽입 해 야 합 니 다. 즉, function 의 arguments 입 니 다. 우 리 는 모두 arguments 가 Array 라 고 생각 할 지 모 르 지만 인 스 턴 트 of 를 사용 하여 테스트 하면 arguments 가 Array 대상 이 아니 라 는 것 을 알 수 있 습 니 다. 비록 비슷 해 보이 지만.
그리고:
var a = new Array () 테스트 하기;if (a instanceof Object) alert('Y');else alert('N');
Y 를 얻다
그러나 (window instanceof Object) alert ('Y');else alert('N');
득 'N'
그래서 여기 인 스 턴 트 오 브 테스트 의 object 는 js 문법 중의 object 를 말 하 며 dom 모델 대상 을 말 하 는 것 이 아 닙 니 다.
type: of 를 사용 하면 차이 가 있 을 수 있 습 니 다.
alert (type: of (window) 는 object 를 얻 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.