I have the following problem. I need to pass the hello function to name and them later call it. But I get the error, hello is not a function.
<html>
<head>
<title>D</title>
<script type="text/javascript">
hello = function(){
alert("hello! I'm called") ;
}
</script>
</head>
<body>
<script type="text/javascript">
name = function( hello ) {
hello() ;
}
name();
</script>
</body>
</html>
-
you hide hello function with argument "hello" that is used in the name function definition. when you invoke name(), you don't pass function. call name as name( hello ); also don't hide gloval names like thisAlexandr– Alexandr2011年10月28日 09:02:33 +00:00Commented Oct 28, 2011 at 9:02
-
@Alexandr Ops! My mistake. But in this case Firefox working fine and chrome does not.Dewsworld– Dewsworld2011年10月28日 09:28:26 +00:00Commented Oct 28, 2011 at 9:28
5 Answers 5
You are defining a parameter called hello within name(), but you are calling name() with no parameter, thus hello is undefined within the context of name(). Either pass the parameter when you call name(), or remove the parameter completely:
name = function (hello) {
hello();
}
name(hello);
or
name = function() {
hello();
}
name();
2 Comments
Inside name, hello will refer to the parameter hello. As you are calling the function without passing any argument, hello is undefined.
Either pass the function as argument
name(hello);
or omit the parameter specification, so that hello will refer to the global variable:
name = function() {
hello();
};
You should avoid having global variables, so that they do not collide with window properties or other libraries. You can use an object as namespace:
var myNameSpace = {};
myNameSpace.hello = function...
myNameSpace.name = function...
Comments
There's two problem with your code.
- You cannot declare a
variablename that is already existing in theWindowclass. - Your name
functioncontains a parameter that is named by thehellofunction. For that you need to pass a function delegate in order to achieve this.
Take a look at the complete code below:
<html>
<head>
<title>D</title>
<script type="text/javascript">
var hello = function(){
alert("hello! I'm called") ;
}
</script>
</head>
<body>
<script type="text/javascript">
var nameFunc = function( helloDelegate ) {
helloDelegate() ;
}
nameFunc(hello);
</script>
</body>
</html>
Comments
I would change it completely to this:
function hello(){
alert("hello! I'm called");
}
function name(hello) {
hello() ;
}
name(hello);
No need to assign the functions to variables like you are doing.
Like others have said, the main problem was that you weren't passing anything into the name function.
2 Comments
hello is useless in name function. No matter what you provide, JS engine will lookup functions first than parameters and evaluate.Dewsworld, just learn how to debug javascript. It'll help you to investigate the problem and find solution yourself. In some browsers debuggers is built-in, in others you have to download it. But it does not matter.
As soon as you start execute your scripts step-by-step, you'll see all its variables current state and it'll be more clear and effective for your than giving reason of the problem you have met.
Also, read JavaScript The Definitive Guide, the last edition. At least try to find solution and reason of this in the book. Cause here you only get short solution, as a rule, without deep undestanding how JavaScript engine works.