0

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>
2
  • 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 this Commented Oct 28, 2011 at 9:02
  • @Alexandr Ops! My mistake. But in this case Firefox working fine and chrome does not. Commented Oct 28, 2011 at 9:28

5 Answers 5

6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Can't you simply do... function() { hello(); }
@Consciousness Actually I have a complex problem in real, I made it easy here :) Thanks for the solution
5

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

2

There's two problem with your code.

  1. You cannot declare a variable name that is already existing in the Window class.
  2. Your name function contains a parameter that is named by the hello function. 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

1

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

Passing the parameter hello is useless in name function. No matter what you provide, JS engine will lookup functions first than parameters and evaluate.
@MunimAbdul Try running that code without passing in the parameter. It doesn't call the hello function.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.