関数オブジェクト[1]のプロパティはコンストラクタプロパティ、インスタンスプロパティ、(インスタンス)prototypeプロパティがある。
function Foo(value) {
this.name = value;
// インスタンスメソッド
// インスタンスごとにメモリを確保する。メモリの面はprototypeメソッドが効率的。
this._getName = function() {
console.log(this.name);
}
}
Foo.getLength = function() {
console.log(Foo.length); // lengthはFooコンストラクタプロパティ
};
Foo.getProto = function() {
console.log(Foo.prototype); // prototypeはコンストラクタプロパティ
};
// prototypeメソッド
Foo.prototype.protoGetName = function() {
console.log(this.name);
};
// コンストラクタメンバ
Foo.getLength(); // 1
Foo.getProto(); // Foo.prototype
// インスタンスメンバ
var foo = new Foo('foo');
foo.getName(); // foo
foo.protoGetName(); // foo
Arrayのコンストラクタプロパティ、prototype(インスタンス)プロパティ、インスタンスプロパティの例。
| コンストラクタプロパティ | prototypeプロパティ | インスタンスプロパティ |
|---|---|---|
| prototype length(値は1) |
constructor, concat, joinなど | 0以上の整数値(値は配列の要素) lentgh(値は配列の要素数) |
コンストラクタプロパティのlengthはコンストラクタの引数の数。Arrayは1つ。
function Foo(x , y, z) {
}
console.log(Foo.length); // 3
[1] この記事の関数オブジェクトはコンストラクタ関数を想定している。
No comments yet.
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。