javascript javascript函数未定义错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17379906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript function undefined error
提问by star
I got stuck in something and I know it might be silly! I try to figure out what the parenthesis ")()" at the end of this code do? jsFiddleSince if I remove them it does not show any thing. I need to add more function in this part of the code but because of the parenthesis I got the errors.
我被什么东西卡住了,我知道这可能很傻!我试图弄清楚这段代码末尾的括号“)()”是做什么的? jsFiddle因为如果我删除它们,它不会显示任何东西。我需要在这部分代码中添加更多功能,但由于括号的原因,我得到了错误。
(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();
}
})()
Thank you!!
谢谢!!
回答by A. Wolff
Immediately-Invoked Function Expression (IIFE
)
立即调用函数表达式 ( IIFE
)
Good reading here: http://benalman.com/news/2010/11/immediately-invoked-function-expression/
在这里阅读很好:http: //benalman.com/news/2010/11/immediately-invoked-function-expression/
You can of course add into it any functions but because of scoping, you can call these functions only in same or deeper scope.
您当然可以向其中添加任何函数,但由于作用域,您只能在相同或更深的范围内调用这些函数。
e.g test() function: http://jsfiddle.net/ZaJZu/
例如 test() 函数:http: //jsfiddle.net/ZaJZu/
回答by Zaheer Ahmed
You defined an anonymous functions. Usually a named function like:
你定义了一个匿名函数。通常是一个命名函数,如:
function myfunc(){
//code
}
can be called:
可以称为:
myfunc();
Exactly this ()
parenthesis are doing.It called the anonymous function on completion. If you don't want these, then named your function and call it from where you need as give example above.
正是这个()
括号在做。它在完成时调用匿名函数。如果你不想要这些,那么命名你的函数并从你需要的地方调用它,如上面的例子。
回答by Peter Richmond
The outer parentheses around the whole thing turn the function into a function expression (as opposed to a function declaration). There are other ways to do that, but the parentheses are the common convention. The () at the end of the function expression is what triggers the immediate function invocation.
整个事物周围的外括号将函数转换为函数表达式(而不是函数声明)。还有其他方法可以做到这一点,但括号是常见的约定。函数表达式末尾的 () 是触发立即函数调用的原因。
This is not a self-invoked anonymous function as that would be a recursive function. The pattern is called an Immediately-Invoked Function Expression (IIFE). It's commonly used in the module pattern, but it's also used relatively often to assign the result of a small, inline function to a variable. Regular old invoked-later function expressions (without the () at the end) are also commonly passed as callbacks or assigned to variables, or used inline in object literals to define methods.
这不是一个自调用的匿名函数,因为那将是一个递归函数。该模式称为立即调用函数表达式 (IIFE)。它通常用于模块模式,但也相对经常用于将小型内联函数的结果分配给变量。常规旧的后调用函数表达式(末尾没有 ())也通常作为回调传递或分配给变量,或在对象字面量中内联用于定义方法。