0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【JavaScript・学習ログ4】クラスの構成

Posted at

クラスとは

オブジェクトの設計図。一度作成すれば、同じ特徴を持つオブジェクトを大量に生産できる。

構成

// クラスを定義する
class Product {
	// コンストラクタ(初期化)
	constructor(name,price,category) {
		this.name = name;
		this.price = price;
		this.category = category;
	}

	//メソッドの定義
	describe() {
		console.log('この商品名は' + this.name + 'です。');
	} 
}

// インスタンス化する
const shampoo = new Product('シャンプー',500,'生活雑貨');
const coffee = new Product('コーヒー',1500,'飲料');

// インスタンスの出力
console.log(shampoo);
console.log(coffee);

// メソッド呼び出し
shampoo.describe();

// 通常のオブジェクトにメソッドを定義する
const user = {
	name:'太郎',
	age: 36,
	gender: '男性',
	greet: () => {
		console.log('よろしくお願いします');
	}
}
// メソッド呼び出し
user.greet();

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?