1

Possible Duplicate:
“Usual” functions vs function variables in JavaScript
whats the difference between function foo(){} and foo = function(){}?

i don't really see the difference between those two functions :

window.send_to_editor = function(html){

and

function send_to_editor(html) {

they both have a name to call them, although the first one is considered to be anonymous ?

Thanks for your answer

1
  • 2
    @Michael: This might be subject to interpretation. The first function is definitely a nameless function which I would say it the same as an anonymous function. The name of the variable the function is assigned to is not the name of the function. You can assign the same function to multiple variables. Commented Mar 19, 2012 at 16:37

1 Answer 1

1

A few things...

  1. The first is creating an anonymous function and then assigning it to a non anonymous member...
  2. The second is actually declaring the function, and declared functions are always hoisted

To add to your confusion.. this is what hoisting means... declared functions are "hoisted" to the top so the following is valid.

//call it
funcOne();

//declare it
function funcOne() {
    alert("Why am I working? I thought javascript was top down?!?!?");
}

Also, you can name function expressions (for the purpose of calling them recursively). This is also valid

var funcOne = function internalName() {
    internalName();
};

I usually prefer creating functions via assignment/expression, mainly because it more accurately describes that functions are first class values, and does not incidentally create possible confusion via hoisting behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks jondavidjohn, it still confuses me, but thanks for the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.