이를 위해 "프로토타입"을 사용하십시오. JavaScript 객체는 프로토타입에서 속성과 메서드를 상속합니다. 변수에 액세스하기 위해 JavaScript에서 "this"도 사용했습니다.
예시
function Customer(fullName){
this.fullName=fullName;
}
Customer.prototype.setFullName = function(newFullName){
this.fullName=newFullName;
}
var customer=new Customer("John Smith");
console.log("Using Simple Method = "+ customer.fullName);
customer.setFullName("David Miller");
console.log("Using Prototype Method = "+customer.fullName); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
여기에서 내 파일 이름은 demo79.js입니다.
출력
이것은 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\JavaScript-code> node demo79.js Using Simple Method = John Smith Using Prototype Method = David Miller