What's the name of Google Analytics async design pattern and where is it used?

前端 未结 4 1120
[愿得一人]
[愿得一人] 2020-12-12 10:38

Google Analytics async code uses a very distinct design pattern for javascript code execution.

The code depends on a library and it doesn\'t know if the library has

4条回答
  •  爱一瞬间的悲伤
    2020-12-12 11:07

    All it's doing is pushing data into a global array

    var _qaq = _qaq || [];
    _qaq.push(stuff);
    

    It basically allows you to buffer up data to deal with before the library has loaded.

    The main reason this pattern isn't used much is that other libraries generally need the resources to load before they can do anything. It wouldn't make any sense to start buffering jQuery commands.

    It's not a pattern it's simply storing data in global scope and saying it's some-one elses job to process this, I don't care when you process it.

    However it is a clever way to deal with the fact you don't know when something is loaded or ready, the common alternative is a DOMReady block.

提交回复
热议问题