Home > Enterprise >  Why I cannot use the function itself in following code?
Why I cannot use the function itself in following code?

Time:02-07

Why the function fn2 cannot call itself? When I call it, the program should be able to find it in the scope of fn, but why the console says fn2 is not defined?

<script >
    var fn={
        fn2:function(){
            console.log(fn2);
        }
    }
    fn.fn2();
    
    
</script>
output:
test.html:64 Uncaught ReferenceError: fn2 is not defined
    at Object.fn2 (test.html:64:25)
    at test.html:67:8

CodePudding user response:

I don't know why you need to console.log a function. but I think if you use this.fn2 instead of fn2 , It would work and It will log the function in console

    var fn={
        fn2:function(){ 
            console.log(this.fn2);
        }
    }
    fn.fn2();

CodePudding user response:

It's talking about fn2 in the console.log the fn2 function did fire.

  •  Tags:  
  • Related