On Sep 2, 1:09 pm, jan wrote:
> Hi, another starter question:
>
> I do not understand why
>
> $('#content').empty().append("<b>hello</b>");
>
> works, and this works
>
> $('#content').empty().append(makeContent());
                               ^^^^^^^^^^^^^

makeContent() - is a function call, where - makeContent - is an
identifier that is resolved against the scope chain to retrieve a
value that is a reference to a function object, and - () - are
effectively call operators that call the function referred to by -
makeContent -.

>
> function makeContent(){
>                 return "<b>hello</b>";
>                 };
>
> and this
>
> $('#content').empty().append( function() { return "<b>hello</b>" } );
<snip>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

function() { return "<b>hello</b>" } - is a function expression, it is
evaluated by creating a function object and the resulting 'value of
the expression' is a reference to that new function object. However,
that function is never called so it never gets a chance to return
"<b>hello</b>". You could call that function by adding the 'call
operator' to the end of it to produce - function() { return "<b>hello</
b>" }() -, but if you do this more than once or twice it would be more
efficient to stick with - makeContent() - and only create a single
function object (or even stick to the original string argument).

Reply via email to