0. 공장 모드
function createPerson(name, age, job) {
let o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function () {
console.log(this.name);
};
return o;
}
let person1 = createPerson("Nicholas", 29, "Software Engineer");
let person2 = createPerson("Greg", 27, "Doctor");
1. 생성자 모드
팩토리 패턴은 객체 식별 문제, 새로 생성 된 객체의 유형을 해결하지 못합니다.
2. 싱글 톤 모드
const CreateSingle = (function () {
let instance;
return function (name, age) {
if (instance) {
return instance;
}
this.name = name;
this.age = age;
return instance = this;
};
})();
CreateSingle.prototype.say = function () {
console.log(this.name);
};
const a = new CreateSingle('ifer', 18);
const b = new CreateSingle('elser', 19);
console.log(a === b);