オブジェクト向け
14678 ワード
オブジェクト向けプログラミング
►モデルになるための青写真を作成する(class) 그 청사진을 바탕으로 한 객체(Object)를 만드는 instance) 프로그래밍 패턴
그 청사진을 바탕으로 한 객체(Object)를 만드는 instance) 프로그래밍 패턴
function Car (brand, name, color) {
//인스턴스가 만들어질 때 실행되는 코드
}
class Car() {
constructor(brand, name, color) {
//인스턴스가 만들어질 때 실행되는 코드
}
}
►newキーワードでクラスのインスタンスを作成できます.let avante = new Car('hyundai', 'avante', 'black');
let mini = new Car('bmw', 'mini', 'white');
let beetles = new Car('volkswagen', 'beetles', 'red');
각각의 인스턴스는 Car라는 클래스의 고유한 속성과, 메소드를 갖습니다
►属性と方法クラスで属性とメソッドを定義し、インスタンスで使用します.
►クラス:属性の定義
ES5
function Car(brand, name, color) {
this.brand = brand;
this.name = name;
this.color = color;
}
ES6
class Car() {
constructor(brand, name, color) {
this.brand = brand;
this.name = name;
this.color = color;
}
}
►クラス:メソッドの定義ES5
function Car(brand, name, color) {/* 생략 */}
Car.prototype.refuel = function() {
// 연료 공급을 구현하는 코드
}
Car.prototype.drive = function() {
// 운전을 구현하는 코드
}
ES6
class Car() {
constructor(brand, name, color) {/* 생략 */}
retuel() {
}
drive() {
}
}
►インスタンスでの使用例ES5
function Car(brand, name, color) {
this.brand = brand; //이 예시에서 this는 avante와 mini를 의미함
this.name = name;
this.color = color;
}
let avante = new Car('hyundai', 'avante', 'black');
let mini = new Car('bmw', 'mini', 'white');
avante.color // black;
avante.name // avante;
mini.brand // bmw;
Car.prototype.drive = function () {
console.log(this.model + '출발!');
}
avante.drive(); // avante출발!!
mini.drive(); // mini출발!!
► prototype? constructor? this?newキーを使用してインスタンスを作成する場合、インスタンスはthis値
►概要
// 이 함수는 constructor(생성자)함수
// Car === class
function Car(brand, name, color) {
this.brand = brand; //this 객체 : 이 예시에서 this는 avante와 mini를 의미함
this.name = name;
this.color = color;
}
// Car.prototype = prototype 객체 : 여기에 속성이나 메소드를 정의할 수 있음
Car.prototype.drive = function () {
console.log(this.name + '가 운전을 시작합니다');
}
//여기서 avante === instance
let avante = new Car('hyundai', 'avante', 'black');
Reference
この問題について(オブジェクト向け), 我々は、より多くの情報をここで見つけました https://velog.io/@feelslikemmmm/객체-지향テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol