javascript

はじめはここから

オブジェクトとは(全体)

オブジェクトとは
全体のかたまり

メソッドとは
実行するスイッチ

プロパティとは
操作の値

例として…
navigatorオブジェクト:webブラウザの本体
documentオブジェクト:webページが表示される部分

オブジェクトの配置

オブジェクトモデル

window navigator
 ┣   document
 ┃    ┣   Anchor
 ┃    ┣   Applet
 ┃    ┣   Area
 ┃    ┣   Form
 ┃    ┃    ┣   Button
 ┃    ┃    ┣   Checkbox
 ┃    ┃    ┣   FileUpload
 ┃    ┃    ┣   Password
 ┃    ┃    ┣   Hidden
 ┃    ┃    ┣   Radio
 ┃    ┃    ┣   Reset
 ┃    ┃    ┣   Select
 ┃    ┃    ┣   Submit
 ┃    ┃    ┣   Text
 ┃    ┃    ┗   Textarea
 ┃    ┣   Image
 ┃    ┗   Link
 ┣   history
 ┗   location

prototypeオブジェクト

コンストラクタの場合、
function Person(h,w){
this.height = h;
this.width = w;
this.getHeight = function(){
alert(this.height);
};
}
と書くところ、

function Person(h,w){
this.height = h;
ths.width = w;
}
//コンストラクタのインスタンスのプロパティのみ記載

Person.porototype.getHeight = function(){
alert(this.height);
};
//Personというコンストラクタのprototypeプロパティに対してメソッドを定義

と書く。

オブジェクトの種類

ビルトインオブジェクト

javascriptがあらかじめ用意しているオブジェクトの一部
■String  //文字列を扱う
■Number //数値を扱う
■Boolean //真(true)と偽(false)を扱う
■Math   //数学的な数値を扱う
■Date   //日時を扱う
■Array   //配列要素を扱う
■RegExp //検索パターンを扱う
■Event  //操作を扱う

カスタムオブジェクト

自分で定義するオブジェクト

ドキュメントオブジェクト

DOM

オブジェクトの生成

var a = {};
※リテラル
var b = new Object();
※コンストラクタ

var a = {
height:160,     // プロパティ名:値
width:50,
getHight: function(){ //メソッド名:値(この場合関数)
alert(this.height);
}
}

これをコンストラクタで書くと

var a = new Object();
a.height = 160;
a.width = 50;
a.getHight = function(){alert(this.height);}

※thisはオブジェクト自身を示す。
this.heightはa.heightと同じ意味

引数を使う

function person(h,w){
this.height = h;
this.width = w;
this.getHeight = function(){
alert(this.height);
}
}