理解javascript代码片段

时间:2015-09-02 03:50:33

标签: javascript

早上好!

我希望这个平台上的某个人可以解释以下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有很好的工作知识,但我无法完全理解这一点!谢谢!

2 个答案:

答案 0 :(得分:5)

基本上就是这个

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 }

答案 1 :(得分:0)

<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>