最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript function passing - Stack Overflow

programmeradmin0浏览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>

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>
Share Improve this question edited Apr 12, 2012 at 5:29 Ian Hunter 9,81413 gold badges62 silver badges79 bronze badges asked Oct 28, 2011 at 8:58 DewsworldDewsworld 14.1k23 gold badges71 silver badges104 bronze badges 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 – Alexandr Commented Oct 28, 2011 at 9:02
  • @Alexandr Ops! My mistake. But in this case Firefox working fine and chrome does not. – Dewsworld Commented Oct 28, 2011 at 9:28
Add a ment  | 

5 Answers 5

Reset to default 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 pletely:

name = function (hello) {
    hello();
}
name(hello);

or

name = function() {
    hello();
}
name();

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...

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 plete 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>

I would change it pletely 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.

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.

发布评论

评论列表(0)

  1. 暂无评论