Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 725047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:17:34+00:00 2026-05-14T06:17:34+00:00

myfunc() runs successfully when called from within the same js file. but it is

  • 0

myfunc() runs successfully when called from within the same js file. but it is undefined (Firebug) when called from an HTML page:

JS file:

$(function() {
    myfunc() {
        alert('inside myfunc');
    }
    alert('outside myfunc');
    myfunc(); //this successfully runs myfunc()
});

HTML:

<script>
$(function() {
    myfunc(); //this doesn't run myfunc(). It's undefined
});
</script>

But when I change myfunc() declaration to:

myfunc = function () { ... }

It’s no longer undefined, and runs successfully.

Sorry for this very noob question, but what just happened? Why did it work when I changed the way I declared the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:17:34+00:00Added an answer on May 14, 2026 at 6:17 am

    It’s a matter of scope.

    In

    $(function() {
        myfunc() {
            alert('inside myfunc');
        }
        alert('outside myfunc');
        myfunc(); //this successfully runs myfunc()
    });
    

    it is only available inside the anonymous function (function() { }), so it would also be unavailable if you were to call it outside of the anonymous function but inside the same js file.

    While if you declare it using

    myfunc = function () { ... }
    

    myfunc is a global variable and the function is available everywhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(function() { var myFunc = { init : function() { alert(I need); document.getElementById(myDiv).innerHTML =
I have a function - myFunc() in class A. There are multiple other classes
The C function myfunc operates on a larger chunk of data. The results are
Via Function: myfunc Parameters: a - First parameter b - First parameter I can
I am currently doing something like this: myFunc(tracer); function tracer(message:String):void{ trace(message); } function myFunc(callback:Function):void{
For example the following code prints { key: 'b' } function myFunc(key, value) {
pg.myfunc = function(){ var i = 1, j = 2; this.selected = 1; xx.newObject
def myfunc(x): y = x y.append('How do I stop Python from modifying x here?')
Is this: ($.fn.myFunc = function() { var Dennis = function() { /*code */ }
Given: x = MyFunc(2); My understanding: The variable x is assigned to the function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.