search

Home  >  Q&A  >  body text

javascript - js中,function 用括号括起来,后接一个节点,是个什么意思?

写代码的时候,碰到了这样的代码,其实里面的都弄明白了,但是那种function用括号括起来再加个节点的语法,表示不能得其要领,求解


            (function basic_time(container) {

                var
                        d1 = [],
                        options,
                        graph,
                        i, x, o;

                data = eval(data);

                for (i = 0; i < data.length; i++) {
                    x = data[i].time;
                    d1.push([x, data[i].val]);
                }

                options = {
                    xaxis: {
                        mode: 'time',
                        labelsAngle: 45
                    },
                    yaxis: {min: 0},
                    selection: {
                        mode: 'x'
                    },
                    HtmlText: false,
                    title: '报表' +tmpGraph 
                };

                // Draw graph with default options, overwriting with passed options
                function drawGraph(opts) {
                    // Clone the options, so the 'options' variable always keeps intact.
                    o = Flotr._.extend(Flotr._.clone(options), opts || {});

                    // Return a new graph.
                    return Flotr.draw(
                            container,
                            [
                                {data: d1, lines: {fill: true}}
                            ], o);
                }

                graph = drawGraph();

                Flotr.EventAdapter.observe(container, 'flotr:select', function(area) {
                    // Draw selected area
                    graph = drawGraph({
                        xaxis: {min: area.x1, max: area.x2, mode: 'time', labelsAngle: 45},
                        yaxis: {min: area.y1, max: area.y2}
                    });
                });

                // When graph is clicked, draw the graph with default area.
                Flotr.EventAdapter.observe(container, 'flotr:click', function() {
                    graph = drawGraph();
                });
            })(document.getElementById("editor-render-0"));

天蓬老师天蓬老师2902 days ago607

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-10 12:47:29

    (function(){})()
    

    好的我们先来看在javascript里()表示的是一个表达式

    那么(function(){})就是一个表达式

    但是我们知道function是一个方法可以运行方法

    在表达式后添加()即

    (function(){})()
    

    代表这个方法立即执行


    如果想给这个立即执行的表达式添加参数怎么办

    (function(s){console.log(s)})(s)
    

    在最后的()即立即执行的方法地方填入参数即可


    需要注意的是这是个表达式(立即执行的方法)所以只有在js或者html内解析到这里的时候才会被执行


    reply
    0
  • 怪我咯

    怪我咯2017-04-10 12:47:29

    http://www.chengxuyuans.com/javascript/41172.html js的执行顺序

    reply
    0
  • 黄舟

    黄舟2017-04-10 12:47:29

    你是指这个 http://segmentfault.com/q/1010000000135703 问题?

    BTW, 提问的智慧

    reply
    0
  • Cancelreply