alex said:
i think both sd and sd1 are objects,isn't it?
Please don't top post, reply below trimmed quotes.
Yes, they are both objects. Anything that isn't a string or number
primitive is an object, even functions. Javascript is very simple in
that regard. It may also be confusing since even the primitives will
be converted (usually temporarily) to objects wherever that seems
appropriate:
var x = 'I am x the string'; // a string primitive
alert(x.length); // but you can use it like a String object
Fun, eh?
John said:
<script>
var sd=function(){
return{
f1:function(){
alert('f1');
},
f2:function(){
alert('f2');
}
}
}();[snip]
sd is a function.No, it isn't. Notice the pair of parentheses after the function expression?
In this case, the enclosing function expression seems rather pointless.
Mike said that because the anonymous outer function just returns the
object literal, it is effectively the same as your original post and
could be written:
var sd = {
f1 : function(){alert('I am f1');},
f2 : function(){alert('I am f2');}
};
and called:
sd.f1(); // shows 'I am f1'
sd.f2(); // shows 'I am f2'
as you discovered in your second post.