A
A
Anton2016-02-01 04:59:21
JavaScript
Anton, 2016-02-01 04:59:21

Why does the error "TypeError: newsList is not a function" occur?

newsList = (news_id) ->
  alert news_id
  return

In the html code I write this:
<script>
  newsList(10);
</script>

In response, I get this error:
TypeError: newsList is not a function. (In 'newsList(10)', 'newsList' is an instance of HTMLDivElement)
(anonymous function)
What's the problem then? Judging by the CoffeeScript documentation, functions are written exactly like this... Well, in JS they are called exactly like that.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-02-01
@hummingbird

This is because newsList will be declared in the local scope, your code will be wrapped in (function() {...}).call(this);
If you want to use it as in your example, you need to explicitly specify it:

window.newsList = (news_id) ->
  alert news_id
  return

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question