单例模式是指一个类,只有一个实例。实现的思路是,创建实例时候加判断,如果有实例则返回,如果没有就new一个,并返回。
第一步: 创建类。
Employees.call(,id,salary) 父类,也可以没有=== function (arg) { 方法
(arg instanceof Array){
console.log( {
console.log(调用的方法,返回菜单
Waiter.prototype.tellCookTheMenu = 调用的方法,拿到做好的菜
Waiter.prototype.serving =.work();
第二步:使用return结果,这里有判断。
'waiter' (!=
第三部:将1、2整合
waiterSingle = (函数,并将执行的结果赋值给waiterSingle
waiter = ;
=== (arg) { 方法
(arg Array){
console.log('finish order dish $记录work' {
console.log('finish serving a dish $记录work'调用的方法,返回菜单
Waiter.prototype.tellCookTheMenu = 调用的方法,拿到做好的菜
Waiter.prototype.serving = .work();
</span><span style="color: #008000">//</span><span style="color: #008000"> 从顾客order<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,拿到点的菜</span>
Waiter.prototype.getMenu = <span style="color: #0000ff">function</span><span style="color: #000000"> (arg) {
</span><span style="color: #0000ff">this</span>.customer =<span style="color: #000000"> arg;
</span><span style="color: #0000ff">this</span>.menu =<span style="color: #000000"> arg.dish;
console.log(</span>'waiter get the menu',<span style="color: #0000ff">this</span><span style="color: #000000">.menu);
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">;
};
</span><span style="color: #0000ff">return</span><span style="color: #000000"> {
name: </span>'waiter'<span style="color: #000000">,getWaiterInstance: </span><span style="color: #0000ff">function</span><span style="color: #000000"> (...arg) {
</span><span style="color: #0000ff">if</span> (!waiter) { <span style="color: #008000">//</span><span style="color: #008000"> 判断如果waiter里没有,则new,并赋值给waiter</span>
waiter = <span style="color: #0000ff">new</span><span style="color: #000000"> Waiter(...arg)
}
</span><span style="color: #0000ff">return</span><span style="color: #000000"> waiter;
}
}
})();
第四步:创建实例方式
waiter = waiterSingle.getWaiterInstance(2,'Lucy',5000);
原文链接:https://www.f2er.com/js/69556.html