admin管理员组文章数量:1225001
I am trying to use d3.js to draw line chart.
Javascript
function lineChart(){
...
}
function update(){
...
var lineChart = lineChart()
.x(d3.scale.linear().domain([2011, 2014]))
.y(d3.scale.linear().domain([0, 1000000]));
...
}
But the console says that Uncaught TypeError: lineChart is not a function
.
How to fix this?
I am trying to use d3.js to draw line chart.
Javascript
function lineChart(){
...
}
function update(){
...
var lineChart = lineChart()
.x(d3.scale.linear().domain([2011, 2014]))
.y(d3.scale.linear().domain([0, 1000000]));
...
}
But the console says that Uncaught TypeError: lineChart is not a function
.
How to fix this?
3 Answers
Reset to default 7You are shadowing your function.
This happens when you declare another variable/function with the same name of an upper one. What you should do is to give another name to the second one declaration just like @andlrc says in his comment.
var lineChartResult = lineChart()...
You can learn more about shadowing in here: Setting & Shadowing Properties
If you declare a variable with the name of an existing function, that function is no longer available within that context (is shadowed by the variable). Change the name of the variable to avoid that naming collision.
This code is equivalente to yours, maybe you can see what is happening:
function lineChart() {...} //function declared
function update() {
var lineChart; // variable created, its value is undefined
lineChart=lineChart() // linechart is not a function, is an undefined value!
}
Another solution, if you don't want to change your variable name, is to qualify the method name with this
to disambiguate it from the variable name that precedes it:
var lineChart = this.lineChart()...
本文标签: jqueryUncaught TypeErroris not a function in javascriptStack Overflow
版权声明:本文标题:jquery - Uncaught TypeError: .. is not a function in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739342847a2159018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var lineChartResult = lineChart()...
– Andreas Louv Commented Apr 26, 2016 at 8:45