繁体   English   中英

理解一个 javascript 代码片段

[英]Understanding a javascript code fragment

早上好!

我希望这个平台上的人可以解释以下 JS 的逐行工作

<script>
    /*<![CDATA[*/
    (function(w, a, b, d, s) {
        w[a] = w[a] || {};
        w[a][b] = w[a][b] || {
            q: [],
            track: function(r, e, t) {
                this.q.push({
                    r: r,
                    e: e,
                    t: t || +new Date
                });
            }
        };
        var e = d.createElement(s);
        var f = d.getElementsByTagName(s)[0];
        e.async = 1;
        e.src = '//flintnet.actonservice.com/cdnr/94/acton/bn/tracker/17118';
        f.parentNode.insertBefore(e, f);
    })(window, 'ActOn', 'Beacon', document, 'script');
    ActOn.Beacon.track(); /*]]>*/
</script>

我对 JS 有很好的工作知识,但无法完全理解这一点! 谢谢!

本质上是这个

window.ActOn = window.Acton || {}; // create ActOn global if it doesn't exist
window.Acton.Beacon = window.Acton.Beacon || { // create Beacon object on ActOn if it doesn't exist
    q: [], // an empty array
    track: function(r, e, t) { // function to push {r: arg1, e: arg2, t: arg3 || now} onto q
        this.q.push({
            r: r,
            e: e,
            t: t || +new Date
        });
    }
};
var e = document.createElement('script'); // create a script element
var f = document.getElementsByTagName('script')[0]; // find the first script in the document
e.async = 1; // set async = "1" on the new script element
e.src = '//flintnet.actonservice.com/cdnr/94/acton/bn/tracker/17118'; // set the source of the script element
f.parentNode.insertBefore(e, f); // insert the script as the top most script on the page - this will fail on a page with no scripts

ActOn.Beacon.track(); // add a "track" to beacon.q = { r: undefined, e: undefined, t: now }
<script>
    // see "Interface CDATASection" at http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-667469212
    /*<![CDATA[*/
    // `window, 'ActOn', 'Beacon', document, 'script'`
    (function(w, a, b, d, s) {
        w[a] = w[a] || {}; // `window['ActOn'] = window['ActOn'] || {}`
        w[a][b] = w[a][b] || { // `window['ActOn']['Beacon'] = {q:[],..}`
            q: [], // empty array
            // do stuff
            track: function(r, e, t) { // `r, e, t` ?
                this.q.push({
                    r: r,
                    e: e,
                    t: t || +new Date
                });
            }
        };
        var e = d.createElement(s); // create `script` element
        var f = d.getElementsByTagName(s)[0]; // first `script` element in document
        e.async = 1; // set `async`: `true`
        // set `e` `src` , load created `script` element
        e.src = '//flintnet.actonservice.com/cdnr/94/acton/bn/tracker/17118';
        f.parentNode.insertBefore(e, f); // insert `e`: `script` before first `script` element
    })(window, 'ActOn', 'Beacon', document, 'script'); // variables
    // call `ActOn.Beacon.track` , defined as property of `window` above
    ActOn.Beacon.track(); /*]]>*/
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM