Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

prototype design pattern typescript

interface IPrototype {
	getClone(): InstanceType<typeof Person>
}

class Person implements IPrototype {
	public name: string
	public age: number
	public hobby: string

	constructor(name: string, age: number, hobby: string) {
		this.name = name
		this.age = age
		this.hobby = hobby
	}

	public getClone(): InstanceType<typeof Person> {
		return this
	}
}

const john: Person = new Person('john doe', 30, 'programming')
console.log(john)

const jane = john.getClone()
jane.name = 'jane doe'
jane.hobby = 'swimming'

console.log(jane)

const ari = john.getClone()
ari.name = 'ari gunawan'
ari.age = 25
ari.hobby = 'music'

console.log(ari)
 
PREVIOUS NEXT
Tagged: #prototype #design #pattern #typescript
ADD COMMENT
Topic
Name
7+5 =