Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ts Singleton pattern

class Singleton {
  private static instance: Singleton;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }
}

const singleton = Singleton.getInstance();
Comment

singleton design pattern typescript

class Person {
	private static instance: Person

	private constructor() {}

	public static getInstance(): Person {
		if (!Person.instance) {
			Person.instance = new Person()
		}
		return Person.instance
	}

	public name(name: string): string {
		return name
	}

	public age(age: number): number {
		return age
	}

	public hobby(hobby: string): string {
		return hobby
	}
}

const res: Person = Person.getInstance()

console.log(`My name is ${res.name('john doe')} and My age is ${res.age(30)} and My hobby is ${res.hobby('programming')}`)
Comment

PREVIOUS NEXT
Code Example
Typescript :: show number with atelast 23 disgits before decmal angular 
Typescript :: Can we nested try statements in java 
Typescript :: Error: "Filesystem" plugin is not implemented on android 
Typescript :: declare function iwth interface typescript 
Typescript :: how to reorder boxplots in ggplot 
Typescript :: 4. In order to have proper integration of the pulse current it is desired that 
Typescript :: convert function to arrow function typescript 
Typescript :: Name all the elements of set Red. Use the proper set notation. 
Typescript :: how to compile in typescript 
Typescript :: extracts lists from list python 
Typescript :: 3 dots react 
Typescript :: stats python 
Typescript :: typescript set 
Typescript :: Enter into postgresql database AS 
Typescript :: 10 elements of gothic literature 
Typescript :: How to separate two similar names from two lists in Python 
Typescript :: UpdateTable operation with the GlobalSecondaryIndexUpdates parameter 
Cpp :: regex match all between parentheses 
Cpp :: std::pair c++ access element 
Cpp :: vector erase not working c++ 
Cpp :: c++ int to qstring 
Cpp :: simple C++ game code 
Cpp :: how to take user input in a client server program in c++ 
Cpp :: c++ convert binary string to decimal 
Cpp :: Tech mahindra coding questions 
Cpp :: qt qchar to lower 
Cpp :: cuda kernel extern shared memory 
Cpp :: qt messagebox 
Cpp :: random in range c++ 
Cpp :: c++ find key in hashmap 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =