모든 언어에는 유형을 변환 할 수 있으며 JavaScript는 예외는 아닙니다. 또한 개발자에게 많은 유형 변환 액세스 방법을 제공합니다. 글로벌 기능을 통해보다 복잡한 데이터 유형을 구현할 수 있습니다.
코드 사본은 다음과 같습니다.
var a = 3;
var b = a + 3;
var c = "학생" + a;
var d = a.tostring ();
var e = a + "";
document.write (typeof (a) + "" + typeof (b) + "" + typeof (c) + "" + typeof (d) + "" + typeof (e));
// 출력 번호 번호 문자열 문자열
유형 변환의 가장 간단한 예
코드 사본은 다음과 같습니다.
var a = b = c = d = e = 4;
var f = a+b+c+d+c.toString ();
document.write (f); <br> // 출력 결과 164
데이터 유형을 문자열로 변환하려면 tostring () JavaScript를 사용하여 문자열로 변환하고 메커니즘 변환을 구현하십시오.
코드 사본은 다음과 같습니다.
var a = 111;
document.writeln (a.tostring (2)+"<br>");
document.writeln (a.tostring (3)+"<br>");
document.writeln (a.tostring (8)+"<br>");
document.writeln (a.tostring (10)+"<br>");
document.writeln (a.tostring (16)+"<br>");
// 실행 결과
//
1101111
11010
157
111
6f
문자열에서 숫자 유형에서 JavaScript는 parseint () 및 parsefloat ()를 사용하여 변환합니다. 이 방법의 이름과 마찬가지로 전자는 문자를 정수로 변환하고 후자는 문자를 부동 소수점 번호로 변환합니다. 이 두 가지 방법을 전송하는 데 문자 만 사용될 수 있으며 그렇지 않으면 NAN으로 변환됩니다. 더 이상 작업이 수행되지 않습니다.
parseint ()는 먼저 subscript 0에서 문자를 확인합니다.이 문자가 유효한 문자 인 경우 문자를 1에서 확인하십시오. 유효한 문자가 아닌 경우 변환을 종료하십시오. 다음 예는 parseint ()의 예입니다.
코드 사본은 다음과 같습니다.
document.writeln (parseint ( "4555.5544")+"<br>");
document.writeln (parseint ( "0.5544")+"<br>");
document.writeln (parseint ( "1221abes5544")+"<br>");
document.writeln (parseint ( "0xc")+"<br>"); // 이진을 직접 변환합니다
document.writeln (parseint ( "[email protected]")+"<br>");
// 실행 결과
4555
0
1221
12
난
Parseint를 사용하면 이진 변환을 쉽게 달성 할 수 있습니다. (parsefloat ()는 parseflaot과 유사하며 여기에는 더 이상 예제가 없습니다.)
코드 사본은 다음과 같습니다.
document.writeln (parseint ( "0421", 8)+"<br>");
document.writeln (parseint ( "0421")+"<br>");
document.writeln (parseint ( "0421", 16)+"<br>");
document.writeln (parseint ( "af", 16)+"<br>");
document.writeln (parseint ( "011", 10)+"<br>");
// 출력 결과
273
421
1057
175
11