JavaScript でのオブジェクト指向プログラミング (OOP) の探索
JavaScript でのオブジェクト指向プログラミング (OOP) の探索
日付: 2024 年 12 月 17 日
オブジェクト指向プログラミング (OOP) は、オブジェクトを使用して現実世界のエンティティをモデル化するパラダイムです。 JavaScript は多用途のプログラミング言語であり、プロトタイプ、ES6 クラス、最新の機能拡張を通じて OOP を強力にサポートします。今日は、JavaScript における OOP の原理と機能について詳しく説明します。
JavaScript における OOP の中心的な概念
1.オブジェクト
オブジェクトは OOP の構成要素です。 JavaScript では、オブジェクトはキーと値のペアのコレクションです。
例: オブジェクトの作成
const car = { brand: "Toyota", model: "Corolla", start() { return `${this.brand} ${this.model} is starting.`; } }; console.log(car.start()); // Output: Toyota Corolla is starting.
2.クラス
クラスはオブジェクトを作成するための設計図です。データと動作をカプセル化します。 JavaScript は ES6 で class キーワードを導入しました。
例: クラスの作成
class Animal { constructor(name, species) { this.name = name; this.species = species; } makeSound() { return `${this.name} is making a sound.`; } } const dog = new Animal("Buddy", "Dog"); console.log(dog.makeSound()); // Output: Buddy is making a sound.
3.カプセル化
カプセル化とは、一部のコンポーネントへの直接アクセスを制限しながら、データとメソッドを一緒にバンドルすることを意味します。 JavaScript は、パブリック、プライベート、および保護されたメンバーを使用してこれを実現します。
プライベートフィールド
プライベート フィールドは # 接頭辞で示され、クラス内でのみアクセスできます。
例: プライベートフィールド
class BankAccount { #balance; constructor(initialBalance) { this.#balance = initialBalance; } deposit(amount) { this.#balance += amount; } getBalance() { return this.#balance; } } const account = new BankAccount(100); account.deposit(50); console.log(account.getBalance()); // Output: 150 // console.log(account.#balance); // Error: Private field '#balance' must be declared in an enclosing class
4.継承
継承により、extends キーワードを使用して、あるクラスが別のクラスからプロパティとメソッドを継承できます。
例: 継承
class Vehicle { constructor(brand) { this.brand = brand; } start() { return `${this.brand} vehicle is starting.`; } } class Car extends Vehicle { constructor(brand, model) { super(brand); // Calls the parent class constructor this.model = model; } display() { return `${this.brand} ${this.model} is ready to go.`; } } const myCar = new Car("Tesla", "Model S"); console.log(myCar.display()); // Output: Tesla Model S is ready to go.
5.ポリモーフィズム
ポリモーフィズムにより、サブクラスは親クラスのメソッドをオーバーライドして特定の実装を提供できます。
例: メソッドのオーバーライド
class Shape { area() { return "Area is not defined."; } } class Circle extends Shape { constructor(radius) { super(); this.radius = radius; } area() { return Math.PI * this.radius ** 2; } } const circle = new Circle(5); console.log(circle.area()); // Output: 78.53981633974483
6.抽象化
抽象化は、実装の複雑さを隠しながら、重要な詳細のみを公開することに重点を置いています。 JavaScript にはネイティブに抽象クラスがありませんが、それらをシミュレートすることができます。
例: 抽象化のシミュレーション
class Animal { constructor(name) { if (this.constructor === Animal) { throw new Error("Abstract class cannot be instantiated directly."); } this.name = name; } makeSound() { throw new Error("Abstract method must be implemented."); } } class Dog extends Animal { makeSound() { return "Bark!"; } } const dog = new Dog("Buddy"); console.log(dog.makeSound()); // Output: Bark! // const animal = new Animal("Some Animal"); // Error: Abstract class cannot be instantiated directly.
7.プロトタイプとプロトタイプ チェーン
JavaScript はプロトタイプベースの言語です。すべてのオブジェクトには、そのプロトタイプと呼ばれる別のオブジェクトへの内部リンクがあります。
例: プロトタイプチェーン
const car = { brand: "Toyota", model: "Corolla", start() { return `${this.brand} ${this.model} is starting.`; } }; console.log(car.start()); // Output: Toyota Corolla is starting.
8.オブジェクトの構成と継承
継承を使用する代わりに、機能を組み合わせてオブジェクトを構成できます。このアプローチにより、深い継承階層の複雑さが回避されます。
例: 構成
class Animal { constructor(name, species) { this.name = name; this.species = species; } makeSound() { return `${this.name} is making a sound.`; } } const dog = new Animal("Buddy", "Dog"); console.log(dog.makeSound()); // Output: Buddy is making a sound.
OOP の主要原則
- DRY (繰り返さない): クラスと継承を通じてコードを再利用します。
- SOLID Principles: スケーラブルで保守可能な OOP コードを作成するためのベスト プラクティスに従います。
実際の例: ユーザー管理システム
ステップ 1: 基本クラスを定義する
class BankAccount { #balance; constructor(initialBalance) { this.#balance = initialBalance; } deposit(amount) { this.#balance += amount; } getBalance() { return this.#balance; } } const account = new BankAccount(100); account.deposit(50); console.log(account.getBalance()); // Output: 150 // console.log(account.#balance); // Error: Private field '#balance' must be declared in an enclosing class
ステップ 2: 機能を拡張する
class Vehicle { constructor(brand) { this.brand = brand; } start() { return `${this.brand} vehicle is starting.`; } } class Car extends Vehicle { constructor(brand, model) { super(brand); // Calls the parent class constructor this.model = model; } display() { return `${this.brand} ${this.model} is ready to go.`; } } const myCar = new Car("Tesla", "Model S"); console.log(myCar.display()); // Output: Tesla Model S is ready to go.
ステップ 3: インスタンスを作成する
class Shape { area() { return "Area is not defined."; } } class Circle extends Shape { constructor(radius) { super(); this.radius = radius; } area() { return Math.PI * this.radius ** 2; } } const circle = new Circle(5); console.log(circle.area()); // Output: 78.53981633974483
練習課題
- 図書館管理システムのクラス階層を作成します。
- 残高用のプライベート フィールドと入出金用のパブリック メソッドを備えた BankAccount クラスを実装します。
- ポリモーフィズムを示す Car や Bike などのサブクラスを含む Vehicle クラスを作成します。
結論
JavaScript の OOP は、クリーンでモジュール化された再利用可能なコードを作成するための強力な方法を提供します。クラス、継承、カプセル化、ポリモーフィズムなどの概念を習得すると、スケーラブルなアプリケーションを構築するための準備が整います。実験を続けてこれらの概念を現実世界の問題に適用して、理解を深めてください。
明日のトピック: JavaScript の 非同期プログラミング について、コールバック、Promise、async/await について詳しく説明します。乞うご期待!
以上がJavaScript でのオブジェクト指向プログラミング (OOP) の探索の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











フロントエンドのサーマルペーパーチケット印刷のためのよくある質問とソリューションフロントエンド開発におけるチケット印刷は、一般的な要件です。しかし、多くの開発者が実装しています...

JavaScriptは現代のWeb開発の基礎であり、その主な機能には、イベント駆動型のプログラミング、動的コンテンツ生成、非同期プログラミングが含まれます。 1)イベント駆動型プログラミングにより、Webページはユーザー操作に応じて動的に変更できます。 2)動的コンテンツ生成により、条件に応じてページコンテンツを調整できます。 3)非同期プログラミングにより、ユーザーインターフェイスがブロックされないようにします。 JavaScriptは、Webインタラクション、シングルページアプリケーション、サーバー側の開発で広く使用されており、ユーザーエクスペリエンスとクロスプラットフォーム開発の柔軟性を大幅に改善しています。

スキルや業界のニーズに応じて、PythonおよびJavaScript開発者には絶対的な給与はありません。 1. Pythonは、データサイエンスと機械学習でさらに支払われる場合があります。 2。JavaScriptは、フロントエンドとフルスタックの開発に大きな需要があり、その給与もかなりです。 3。影響要因には、経験、地理的位置、会社の規模、特定のスキルが含まれます。

この記事の視差スクロールと要素のアニメーション効果の実現に関する議論では、Shiseidoの公式ウェブサイト(https://www.shisido.co.co.jp/sb/wonderland/)と同様の達成方法について説明します。

JavaScriptの最新トレンドには、TypeScriptの台頭、最新のフレームワークとライブラリの人気、WebAssemblyの適用が含まれます。将来の見通しは、より強力なタイプシステム、サーバー側のJavaScriptの開発、人工知能と機械学習の拡大、およびIoTおよびEDGEコンピューティングの可能性をカバーしています。

同じIDを持つ配列要素をJavaScriptの1つのオブジェクトにマージする方法は?データを処理するとき、私たちはしばしば同じIDを持つ必要性に遭遇します...

さまざまなJavaScriptエンジンは、各エンジンの実装原則と最適化戦略が異なるため、JavaScriptコードを解析および実行するときに異なる効果をもたらします。 1。語彙分析:ソースコードを語彙ユニットに変換します。 2。文法分析:抽象的な構文ツリーを生成します。 3。最適化とコンパイル:JITコンパイラを介してマシンコードを生成します。 4。実行:マシンコードを実行します。 V8エンジンはインスタントコンピレーションと非表示クラスを通じて最適化され、Spidermonkeyはタイプ推論システムを使用して、同じコードで異なるパフォーマンスパフォーマンスをもたらします。

フロントエンドのVSCodeと同様に、パネルドラッグアンドドロップ調整機能の実装を調べます。フロントエンド開発では、VSCODEと同様のVSCODEを実装する方法...
