Yanor.net/
Wiki
Blog
GitHub
Sandbox
開始行:
* 関数オブジェクトを使ったクラス作成 - 応用 [#ee1faea0]
** クラスの作成 [#u48ec2eb]
*** 1. 空のコンストラクタ [#ga5b2288]
var Animal = function() {};
- 関数オブジェクトをクラスのコンストラクタに見立てる。
*** 2. コンストラクタでメソッドやフィールドを定義[#fc4484...
var Animal = function(name, age) {
this.name = name;
this.age = age;
this.sayName = function() {
alert("My name is " + this.name);
};
};
- thisを使ってクラスのプロパティにアクセスする。
- ただし、この方法はインスタンス生成ごとにメソッドを定義...
*** 3. プロトタイプでメソッドを定義[#n7668f55]
var Animal = function(name, age) {
this.name = name;
this.age = age;
};
Animal.prototype = {
sayName: function() {
alert("My name is " + this.name);
}
};
:プロトタイプ汚染について|この方法でArrayのような組込みク...
*** 4. プロパティもプロトタイプに乗せたい場合 [#zca9d352]
var Animal = function(name, age) {
this.init.apply(this, arguments);
};
Animal.prototype.init = function (name, age) {
this.name = name;
this.age = age;
sayName: function() {
alert("My name is " + this.name);
}
};
- 初期化メソッド(ここではinit())を作って、コンストラク...
- apply()とは [[JavaScript/文法/関数/call()とapply()]]。
** インスタンスを生成・メソッドを利用 [#m5dce2e9]
var a = new Animal("Taro", 10);
a.sayName();
** 継承 [#w9c0d0c2]
*** 子クラスの作成 1 [#z6e09049]
var Dog = function(name, age) {
this.name = name;
this.age = age;
};
Dog.prototype = new Animal;
Dog.prototype.sayAge = function() {
alert("My age is " + this.age);
}
- Dog.prototype = new Animal で継承を実現している。
- Dog.prototype.sayAge = function()... でメソッドを子クラ...
*** 子クラスのインスタンスを作成・メソッドを利用 [#ccee75...
var d = new Dog("Jiro", 5);
d.sayAge();
*** 子クラスの作成 2(子クラスのコンストラクタで親クラス...
var Dog = function(name, age) {
Animal.call(this, name, age);
};
Dog.prototype = new Animal;
Dog.prototype.sayAge = function() {
alert("My age is " + this.age);
}
- 子クラスの作成 1ではコンストラクタ内での処理を書き直す...
- call()とは [[JavaScript/文法/関数/call()とapply()]]。
*** prototypeのconstructorプロパティ [#y0fa63a1]
↑のままではDogクラスのprototypeのconstructorプロパティはA...
Dog.prototype = new Animal;
Dog.prototype.constructor = Dog;
終了行:
* 関数オブジェクトを使ったクラス作成 - 応用 [#ee1faea0]
** クラスの作成 [#u48ec2eb]
*** 1. 空のコンストラクタ [#ga5b2288]
var Animal = function() {};
- 関数オブジェクトをクラスのコンストラクタに見立てる。
*** 2. コンストラクタでメソッドやフィールドを定義[#fc4484...
var Animal = function(name, age) {
this.name = name;
this.age = age;
this.sayName = function() {
alert("My name is " + this.name);
};
};
- thisを使ってクラスのプロパティにアクセスする。
- ただし、この方法はインスタンス生成ごとにメソッドを定義...
*** 3. プロトタイプでメソッドを定義[#n7668f55]
var Animal = function(name, age) {
this.name = name;
this.age = age;
};
Animal.prototype = {
sayName: function() {
alert("My name is " + this.name);
}
};
:プロトタイプ汚染について|この方法でArrayのような組込みク...
*** 4. プロパティもプロトタイプに乗せたい場合 [#zca9d352]
var Animal = function(name, age) {
this.init.apply(this, arguments);
};
Animal.prototype.init = function (name, age) {
this.name = name;
this.age = age;
sayName: function() {
alert("My name is " + this.name);
}
};
- 初期化メソッド(ここではinit())を作って、コンストラク...
- apply()とは [[JavaScript/文法/関数/call()とapply()]]。
** インスタンスを生成・メソッドを利用 [#m5dce2e9]
var a = new Animal("Taro", 10);
a.sayName();
** 継承 [#w9c0d0c2]
*** 子クラスの作成 1 [#z6e09049]
var Dog = function(name, age) {
this.name = name;
this.age = age;
};
Dog.prototype = new Animal;
Dog.prototype.sayAge = function() {
alert("My age is " + this.age);
}
- Dog.prototype = new Animal で継承を実現している。
- Dog.prototype.sayAge = function()... でメソッドを子クラ...
*** 子クラスのインスタンスを作成・メソッドを利用 [#ccee75...
var d = new Dog("Jiro", 5);
d.sayAge();
*** 子クラスの作成 2(子クラスのコンストラクタで親クラス...
var Dog = function(name, age) {
Animal.call(this, name, age);
};
Dog.prototype = new Animal;
Dog.prototype.sayAge = function() {
alert("My age is " + this.age);
}
- 子クラスの作成 1ではコンストラクタ内での処理を書き直す...
- call()とは [[JavaScript/文法/関数/call()とapply()]]。
*** prototypeのconstructorプロパティ [#y0fa63a1]
↑のままではDogクラスのprototypeのconstructorプロパティはA...
Dog.prototype = new Animal;
Dog.prototype.constructor = Dog;
ページ名: