suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

<code class="lang-javascript">

            (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"));

 

</code>

天蓬老师天蓬老师2905 Tage vor617

Antworte allen(3)Ich werde antworten

  • 大家讲道理

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

    1

    2

    <code>(function(){})()

    </code>

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

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

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

    在表达式后添加()即

    1

    2

    <code>(function(){})()

    </code>

    代表这个方法立即执行


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

    1

    2

    <code>(function(s){console.log(s)})(s)

    </code>

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


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


    Antwort
    0
  • 怪我咯

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

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

    Antwort
    0
  • 黄舟

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

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

    BTW, 提问的智慧

    Antwort
    0
  • StornierenAntwort