Reputation: 13
A = {
f1: function() {
return {
a: function(){ alert('sss'); }
}
}
}
A.f1().a();
Why is it used in this way?
Why is the method a()
bound to A.f1()?
Upvotes: 0
Views: 81
Reputation: 1767
When you do as below:
var x = A.f1();
What you get on to x is the object returned by the f1 function. which is:
{
a: function(){ alert('sss'); }
}
now the object 'x' has function a() on it. You can call that function as:
x.a();
Which is exatly similar to:
A.f1().a();
Upvotes: 0
Reputation: 96865
Member function f1
of A
returns an object literal with its member a
set to a function. It could've also been written as:
A = {
f1: {
a: function() { alert('sss'); }
}
}
A.f1.a();
Returning an object could in this case be personal preference. There is no functional difference between these two examples.
Upvotes: 1