[JavaScript]コンストラクタ
887 ワード
コンストラクタ
複数の類似オブジェクトを作成する必要がある場合?
function User(name, age){
this.name = name;
this.age = age;
}
let user1 = new User('Mike', 30);
let user2 = new User('Jane', 22);
let user3 = new User('Tom', 17);
function User(name, age){
this.name = name;
this.age = age;
}
let user1 = new User('Mike', 30);
let user2 = new User('Jane', 22);
let user3 = new User('Tom', 17);
コンストラクション関数の動作
さぎょうモード
function User(name, age){
this = {} // 생략가능 코드
this.name = name;
this.age = age;
return this; // 생략가능 코드
}
new 함수명(); // 실행 함수
コンストラクタメソッドの追加 function User(name, age){
this.name = name;
this.age = age;
this.sayName = function(){
console.log(this.name); // this 는 user5를 가르킨다.
}
}
let user5 = new User('Han', 40);
user5.sayName(); // 'Han'
Reference
この問題について([JavaScript]コンストラクタ), 我々は、より多くの情報をここで見つけました
https://velog.io/@tmdckzm/자바스크립트-생성자-함수
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
function User(name, age){
this = {} // 생략가능 코드
this.name = name;
this.age = age;
return this; // 생략가능 코드
}
new 함수명(); // 실행 함수
function User(name, age){
this.name = name;
this.age = age;
this.sayName = function(){
console.log(this.name); // this 는 user5를 가르킨다.
}
}
let user5 = new User('Han', 40);
user5.sayName(); // 'Han'
Reference
この問題について([JavaScript]コンストラクタ), 我々は、より多くの情報をここで見つけました https://velog.io/@tmdckzm/자바스크립트-생성자-함수テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol