special operator about function

A

alex

<script>
var sd=function(){
return{
f1:function(){
alert('f1');
},
f2:function(){
alert('f2');
}
}
}();
sd.f2();
</script>

when execute sd.f2();it will alert f1,who can tell me why?
 
J

John G Harris

alex said:
<script>
var sd=function(){
return{
f1:function(){
alert('f1');
},
f2:function(){
alert('f2');
}
}
}();
sd.f2();
</script>

when execute sd.f2();it will alert f1,who can tell me why?

sd is a function. Shouldn't you be calling it ?

sd().f2();

John
 
M

Michael Winter

John said:
alex 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.

[snip]

Mike
 
R

RobG

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.
 
J

John G Harris

Michael said:
John said:
alex 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?

No, I didn't notice. The layout of curly brackets is so awful it's too
easy to mis-read, especially as it does something highly unlikely. To
make it more confusing, one of his 3 posts has no final round brackets.
Perhaps that's the one I was replying to.

In this case, the enclosing function expression seems rather pointless.

To put it mildly.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top