Number javascript 入門

Number  javascript 入門

Number

Numberオブジェクトについて解説します。

宣言
変数名 = new Number値);

代入
変数名 = 値;

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”UTF-8″>
<title>sample1</title>

<script type=”text/javascript”>

function helloworld(){

num = new Number(100);
document.write(num);
}

</script>
</head>
<body>
<form>

<input type=”button” value=”ここをクリック” onClick=” helloworld()”>

</form>

</body>
<html>

実行結果は100です。

Number constructor プロパティ

constructor プロパティはコンストラクタを参照します。

変数名.constructor

実行結果はfunction Number() { [native code] }

prototype プロパティ

prototype プロパティは配列にプロパティを作成する。
Number.prototype.プロパティ名 = 値;

MAX_VALUE プロパティ

MAX_VALUEは最大の値です。

この値は固定値で決まっています。

オブジェクトに対してではなく

Number.MAX_VALUEで使用します。

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”UTF-8″>
<title>sample1</title>

<script type=”text/javascript”>

function helloworld(){

num = new Number(100);

document.write(Number.MAX_VALUE);
}

</script>
</head>
<body>
<form>

<input type=”button” value=”ここをクリック” onClick=” helloworld()”>

</form>

</body>
<html>

実行結果は1.7976931348623157e+308です。

MIN_VALUE プロパティ

MIN_VALUEは最小の値です。

この値は固定値で決まっています。

オブジェクトに対してではなく

Number.MIN_VALUEで使用します。

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”UTF-8″>
<title>sample1</title>

<script type=”text/javascript”>

function helloworld(){

num = new Number(100);
document.write(Number.MIN_VALUE);
}

</script>
</head>
<body>
<form>

<input type=”button” value=”ここをクリック” onClick=” helloworld()”>

</form>

</body>
<html>

実行結果は5e-324です。

toString メソッド

toStringは引数に数字を指定するとN進数になります。

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”UTF-8″>
<title>sample1</title>

<script type=”text/javascript”>

function helloworld(){

num = new Number(100);
document.write(num.toString(2));
}

</script>
</head>
<body>
<form>

<input type=”button” value=”ここをクリック” onClick=” helloworld()”>

</form>

</body>
<html>

実行結果は1100100です。

valueOf メソッド

valueOfは値を取得します。

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”UTF-8″>
<title>sample1</title>

<script type=”text/javascript”>

function helloworld(){

num = new Number(100);
document.write(num.valueOf());
}

</script>
</head>
<body>
<form>

<input type=”button” value=”ここをクリック” onClick=” helloworld()”>

</form>

</body>
<html>

実行結果は100です。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Translate »