We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
个人写法不习惯用class语法糖
const danli = function (name) { this.name = name; this.instance = null; }; danli.prototype.getName = function () { console.log(this.name); }; danli.getInstance = function (ret) { if (!this.instance) { this.instance = new danli(ret); //假如为null,创建一个构造函数,此时instance标记不为空,不用进行二次创建 } return this.instance; }; let a = danli.getInstance("a"); let b = danli.getInstance("b"); a.getName(); b.getName(); console.log(a === b); //他俩相同,因为没有二次创建
The text was updated successfully, but these errors were encountered:
ES6 class实现
class Singleton { constructor(name, age) { if (!Singleton.instance) { this.name = name this.age = age Singleton.instance = this } return Singleton.instance } } console.log(new Singleton("Taobao", 18) === new Singleton("Baidu", 15)) // true
Sorry, something went wrong.
No branches or pull requests
个人写法不习惯用class语法糖
The text was updated successfully, but these errors were encountered: