**** Artopix.net ****

関数の定義

JavaScriptでは関数の定義とオブジェクトのメソッドの定義は同じになります。どちらもfunction()を使って機能を定義します。関数の基本的な書式は以下のようになります。

function 関数名(パラメータ){ 処理内容 }

関数名は英文字で始まるのが基本ですが、$や_などの記号も使うことができます。また関数名は省略すること匿名関数/無名関数となります。

JavaScriptでは関数のパラメータは引数とも呼ばれ、カンマで区切って複数指定することができます。パラメータを省略した場合でもargumentsにより任意の数、任意の位置のパラメータ値を取得することができます。このため、Java言語などと異なり自由度の高いポリモーフィズムが手軽に処理できます。


Note: 配列の生成

JavaScriptでは多くの言語同様に配列を利用することができます。C言語等では配列には個数を指定したり、代入できるデータ等に制限がありますが、JavaScriptの場合には何の制限もありません。個数も特に指定しなくても動的に割り当てられます。また、どんな型のデータでも代入することができます。実際にはデータだけでなくオブジェクトでも関数でも、ほぼ制限なく代入することができます。

JavaScriptで配列を生成するには、いくつかの書き方があります。

a = new Array();
document.write(typeof(a));
a = new Array;
document.write(typeof(a));

同様に以下のように記述することもできます。

a = [];
document.write(typeof(a));
a = new Array(9);
document.write(a.length);

JavaScriptではC言語等と異なり自動的に要素数が調整されるので、個数を指定しません

a = new Array(9,8,7);
document.write(a.length);

new Array()のパラメータが1つでも数値でない場合は最初の要素として代入されます。

a = new Array("Sample");
document.write(a.length);
a = [9];
document.write(a.length+"<br>");
document.write(a[0]);

JavaScriptは標準では多次元配列に関する命令などが用意されていませんが、配列がオブジェクトであることを利用して、オブジェクト内にオブジェクトを生成することで多次元配列を扱う事ができます。

a = [[1,2,3],[4,5,6],[7,8,9]];
document.write(a.length+"<br>");
document.write(a[2][1]);

生成した配列を削除することもできます。削除すると何もなくなるので結果はundefinedとして表示されます。

test = {};
test.a = [[1,2,3],[4,5,6],[7,8,9]];
document.write(test.a.length+"<br>");
delete test.a;
document.write(test.a+"<br>");

Show Examples 1, 2, 3 | Hide Examples 1, 2, 3

Example 1 (individual):

[|||||||||||||||||] Slide Down || Slide Up

Example 2 (individual):

[|||||||||||||||||] Slide Down || Slide Up

Example 3 (individual):

[|||||||||||||||||] Slide Down || Slide Up

Example 4 (part of group "pets"):

[|||||||||||||||||] Slide Down || Slide Up
The cat (Felis catus), also known as the domestic cat or house cat to distinguish it from other felines, is a small carnivorous species of crepuscular mammal that is often valued by humans for its companionship and its ability to hunt vermin. It has been associated with humans for at least 9,500 years. A skilled predator, the cat is known to hunt over 1,000 species for food. It can be trained to obey simple commands.

Example 5 (part of group "pets"):

[|||||||||||||||||] Slide Down || Slide Up
The dog (Canis lupus familiaris) is a domesticated subspecies of the wolf, a mammal of the Canidae family of the order Carnivora. The term encompasses both feral and pet varieties and is also sometimes used to describe wild canids of other subspecies or species. The domestic dog has been one of the most widely kept working and companion animals in human history, as well as being a food source in some cultures.

Example 6 (part of group "pets"):

[|||||||||||||||||] Slide Down || Slide Up
Rabbits are ground dwellers that live in environments ranging from desert to tropical forest and wetland. Their natural geographic range encompasses the middle latitudes of the Western Hemisphere. In the Eastern Hemisphere rabbits are found in Europe, portions of Central and Southern Africa, the Indian subcontinent, Sumatra, and Japan.

Names
Places
Tags1
Tags2
Tags3
Tags4