From 6ef1fa4618e08426b874529619a66adbd3d1fcf0 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:07:59 +0100 Subject: unslug ja: move --- .../defining_methods/index.html | 39 ---------------------- 1 file changed, 39 deletions(-) delete mode 100644 files/ja/web/javascript/guide/creating_new_objects/defining_methods/index.html (limited to 'files/ja/web/javascript/guide/creating_new_objects/defining_methods') diff --git a/files/ja/web/javascript/guide/creating_new_objects/defining_methods/index.html b/files/ja/web/javascript/guide/creating_new_objects/defining_methods/index.html deleted file mode 100644 index 74731a99d1..0000000000 --- a/files/ja/web/javascript/guide/creating_new_objects/defining_methods/index.html +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: メソッドの定義 -slug: Web/JavaScript/Guide/Creating_New_Objects/Defining_Methods ---- -

メソッドの定義

-

メソッドとはあるオブジェクトに結びつけられた関数のことです。メソッドは、通常の関数の定義と同じ方法で定義します。既存のオブジェクトに関数を結びつけるには次の構文を使用します。

-
object.methodname = function_name
-
-

ここで、object は既存のオブジェクトを、methodname はメソッドに割り当てる名前を、function_name は関数の名前をそれぞれ表しています。

-

すると、次のようにしてオブジェクトのコンテキストでそのメソッドを呼び出すことができます。

-
object.methodname(params);
-
-

オブジェクトのコンストラクタ関数にメソッドの定義を含めることで、あるオブジェクトの種類についてのメソッドを定義することができます。例えば、以前に定義した car オブジェクトのプロパティを整形して表示する関数を定義します。

-
function displayCar() {
-   var result = "A Beautiful " + this.year + " " + this.make
-      + " " + this.model;
-   pretty_print(result);
-}
-
-

pretty_print は水平方向の罫線と文字列を表示する関数です。this を使用してそのメソッドを抱えているオブジェクトを参照しています。

-

次の文

-
this.displayCar = displayCar;
-
-

をオブジェクトの定義に加えることで、この関数を car のメソッドにすることができます。そうすると、car の完全な定義は次のようになります。

-
function car(make, model, year, owner) {
-   this.make = make;
-   this.model = model;
-   this.year = year;
-   this.owner = owner;
-   this.displayCar = displayCar;
-}
-
-

すると、次のようにして各オブジェクトについて displayCar メソッドを呼び出すことができます。

-
car1.displayCar()
-car2.displayCar()
-
-

こうすると次の図のような出力が得られます。

-

Image:obja.gif 図 7.1:メソッドの出力の表示

-

{{ PreviousNext("JavaScript/Guide/Creating_New_Objects/Defining_Properties_for_an_Object_Type", "JavaScript/Guide/Creating_New_Objects/Using_this_for_Object_References") }}

-- cgit v1.2.3-54-g00ecf