我正在尝试实现单例模式,以便在文件中实例化对象并在另一个文件中重用相同的实例。很明显,我不理解一些基本的东西。有什么方向可以更好地理解吗?
let editorDiv = document.createElement('div');
editorDiv.setAttribute('id','editor');
document.body.appendChild(editorDiv);
//a.js
function singleton(){
let _singleton;
let quillOptions = { modules: { toolbar: true },
placeholder: 'Compose an epic...',
theme: 'snow'
}
if (!_singleton) {
_singleton = new Quill('#editor', quillOptions)
}
return _singleton;
}
//b.js
//instantiate
let quill = singleton();
//c.js
//get instance in order to use api
let quillInstace = singleton();
//Ex: quillInstance.getText();
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>