Javascript – Algorithm (4)

How to create a singleton class/ object?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export default class MyClass {
static instance: MyClass | undefined;
static getInstance() {
if (!MyClass.instance) {
MyClass.instance = new MyClass();
}
return this.instance;
}
...
}
export default class MyClass { static instance: MyClass | undefined; static getInstance() { if (!MyClass.instance) { MyClass.instance = new MyClass(); } return this.instance; } ... }
export default class MyClass {

    static instance: MyClass | undefined;

    static getInstance() {
        if (!MyClass.instance) {
            MyClass.instance = new MyClass();
        }

        return this.instance;
    }

    ...
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import MyClass from './MyClass';
const myClass = MyClass.getInstance();
import MyClass from './MyClass'; const myClass = MyClass.getInstance();
import MyClass from './MyClass';

const myClass = MyClass.getInstance();

or

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class MyClass {
...
}
let instance;
export function getMyClassInstance() {
if (instance == null) {
instance = new MyClass();
}
return instance;
}
class MyClass { ... } let instance; export function getMyClassInstance() { if (instance == null) { instance = new MyClass(); } return instance; }
class MyClass {
    ...
}

let instance;
export function getMyClassInstance() {
    if (instance == null) {
        instance = new MyClass();
    }

    return instance;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import { getMyClassInstance } from './MyClass';
const myClass = getMyClassInstance();
import { getMyClassInstance } from './MyClass'; const myClass = getMyClassInstance();
import { getMyClassInstance } from './MyClass';

const myClass = getMyClassInstance();

How to use getter/ setter in class?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(newName) {
this._name = newName;
}
}
class Person { constructor(name) { this._name = name; } get name() { return this._name; } set name(newName) { this._name = newName; } }
class Person {
    constructor(name) {
        this._name = name;
    }

    get name() {
        return this._name;
    }

    set name(newName) {
        this._name = newName;
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
class Person { constructor(name) { this._name = name; } get name() { return this._name; } }
class Person {
    constructor(name) {
        this._name = name;
    }
    get name() {
        return this._name;
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const myName = person.name;
const myName = person.name;
const myName = person.name;

Be the first to comment

Leave a Reply

Your email address will not be published.


*