javascript函数未定义错误

javascript函数未定义错误,javascript,function,undefined,Javascript,Function,Undefined,我被什么东西卡住了,我知道这可能很愚蠢! 我试图弄清楚代码末尾的括号“)()”是做什么的? 因为如果我移除它们,它不会显示任何东西。我需要在这部分代码中添加更多函数,但由于括号的缘故,我出现了错误 (function () { var n = 143, duration = 750, now = new Date(Date.now() - duration), count = 0, data = d3.range(n).m

我被什么东西卡住了,我知道这可能很愚蠢! 我试图弄清楚代码末尾的括号“)()”是做什么的? 因为如果我移除它们,它不会显示任何东西。我需要在这部分代码中添加更多函数,但由于括号的缘故,我出现了错误

(function () {

    var n = 143,
        duration = 750,
        now = new Date(Date.now() - duration),
        count = 0,
        data = d3.range(n).map(function () {
            return 0;
        });

    var margin = {
        top: 6,
        right: 0,
        bottom: 20,
        left: 40
    },
    width = 560 - margin.right,
        height = 120 - margin.top - margin.bottom;

    var x = d3.time.scale()
        .domain([now - (n - 2) * duration, now - duration])
        .range([0, width]);

    var y = d3.scale.linear()
        .range([height, 0]);

    var line = d3.svg.line()
        .interpolate("basis")
        .x(function (d, i) {
        return x(now - (n - 1 - i) * duration);
    })
        .y(function (d, i) {
        return y(d);
    });

    var svg = d3.select("body").append("p").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .style("margin-left", -margin.left + "px")
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    svg.append("defs").append("clipPath")
        .attr("id", "clip")
        .append("rect")
        .attr("width", width)
        .attr("height", height);

    var axis = svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(x.axis = d3.svg.axis().scale(x).orient("bottom"));

    var path = svg.append("g")
        .attr("clip-path", "url(#clip)")
        .append("path")
        .data([data])
        .attr("class", "line");

    tick();

    d3.select(window)
        .on("scroll", function () {
        ++count;
    });

    function tick() {

        // update the domains
        now = new Date();
        x.domain([now - (n - 2) * duration, now - duration]);
        y.domain([0, d3.max(data)]);

        // push the accumulated count onto the back, and reset the count
        data.push(Math.random()*10);
        count = 0;

        // redraw the line
        svg.select(".line")
            .attr("d", line)
            .attr("transform", null);

        // slide the x-axis left
        axis.transition()
            .duration(duration)
            .ease("linear")
            .call(x.axis);

        // slide the line left
        path.transition()
            .duration(duration)
            .ease("linear")
            .attr("transform", "translate(" + x(now - (n - 1) * duration) + ")")
            .each("end", tick);

        // pop the old data point off the front
        data.shift();

    }
})()

谢谢

立即调用的函数表达式(
IIFE

这里读得好:

当然,您可以向其中添加任何函数,但由于作用域,您只能在相同或更深的范围内调用这些函数


e、 g test()函数:

您定义了一个匿名函数。通常是一个命名函数,如:

function myfunc(){
   //code
}
可称为:

myfunc();
括号就是这样做的。它在完成时调用了匿名函数。如果您不需要这些,那么将函数命名并从需要的位置调用它,如上面的示例所示


围绕整件事的外圆括号将函数转换为函数表达式(与函数声明相反)。还有其他方法可以做到这一点,但括号是常见的惯例。函数表达式末尾的()是触发立即函数调用的对象


这不是一个自调用的匿名函数,因为它是一个递归函数。该模式称为立即调用函数表达式(IIFE)。它通常用于模块模式,但也相对经常用于将小型内联函数的结果分配给变量。常规的旧的后调用函数表达式(末尾不带())也通常作为回调传递或分配给变量,或在对象文本中内联使用以定义方法。

底部的括号
()
调用匿名函数。如果要在匿名函数之后添加函数(括号之后)然后在这之后添加一个分号,在新行中您可以定义您的函数
“因为括号中有错误”
-什么错误?是什么代码生成了错误?感谢您的回复:)现在我得到了名为myfunction myfunc()的itI,并调用了它,而不是使用此匿名函数,但当我添加另一个函数时,仍然看到错误“Uncaught SyntaxError:输入意外结束”。我在myfunc()之后添加了新函数!!用包含新函数的代码问另一个问题假设我想添加如下内容:d3.tsv(“data/dataMulti.tsv”,function(error,data){color.domain(d3.keys(data[0]).filter(function(key){return key!=“date”;});而不是test();我写函数时仍然会收到错误!!