What is the difference between Var foo= {}; Foo.method () and singleton mode

Source: Internet
Author: User

var foo={}        Foo.method = function () {            var i = 0;            return {                a:function () {                    console.log (' AA ', i++);                },                b:function () {                    console.log (' BB ', i++);                }            }        }        Foo.method (). A ();        Foo.method (). b ();
var bar= (function () {            var instance;            function init () {                var i =0;                return {                    a:function () {                        console.log (' cc ', i++);                    },                    b:function () {                        console.log (' ee ', i++);                    }                }            }            return {                method:function () {                    if (!instance) {                        instance = init ()                    }                    return instance;                }            }        }());        Bar.method (). A ();        Bar.method (). b ();

  

In the first way, two calls method() re-created the returned object, re-initialized each time the object was created, i so it is called a() and b() has no relationship, and there is no continuity.

The second way is equivalent to the execution of the results 缓存 , the advantage is to cache the benefits (faster reading again), the downside is the downside of the cache (Resource/memory more, longer).

       var foo={} Foo.method = function () {var i = 0;                return {a:function () {console.log (' AA ', i++);                }, B:function () {console.log (' BB ', i++);        }}} var foo = Foo.method ();        FOO.A ();                FOO.B ();            var bar= (function () {var instance;                function init () {var i = 0;                    return {a:function () {console.log (' cc ', i++);                    }, B:function () {console.log (' ee ', i++); }}} return {method:function () {if (!instance)                {instance = init ()} return instance;        }            }        }());        Bar.method (). a (); Bar.methOD (). b ();     

Caching the results of the Foo.method () method through Foo all FOO.B () is modified on the result of FOO.A ()

Both of these ways can have the same result

What is the difference between Var foo= {}; Foo.method () and singleton mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.