By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Question on Object oriented Programming in JS

P: n/a
QQ
Hi All

When I looked at ExtJS source code, I found something strange.

could anyone here explain to me?

In ext-base.js file
they wrap the all code inside:

(function() {
....
....
....

)()
What is that mean?

Thanks
Steven
Jul 7 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
QQ wrote:
could anyone here explain to me?
they wrap the all code inside:

(function() {
...
...
...

)()

What is that mean?
It's declaring an anonymous function (inside the outer parentheses) and
then calling it (via the second pair of parentheses). The idea is to be
able to run a block of code as if it was running inline, but having the
advantage of running all that code in the namespace of the function and
therefore not polluting/corrupting the global namespace. So it can
declare lots of variables named for example, a, i, x and it can be sure
that it's not overwriting any global a, i or x variables. If that code
were just running inline, it would have to ensure it didn't clash with
any global variable names, and it would leave all those variables behind.
Jul 7 '08 #2

P: n/a
QQ <ch**********@gmail.comwrites:
In ext-base.js file
they wrap the all code inside:

(function() {
...
...
...

)()
What is that mean?
That means: create a function expression and then immediately execute
it. The main reason to do that is to limit the scope of variables that
shouldn't be global.

(function() {
var a =1;
// a is defined here
})();
// a isn't defined here

Nothing to do with OO.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 7 '08 #3

P: n/a
On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.comwrote:
Hi All

When I looked at ExtJS source code, I found something strange.

could anyone here explain to me?

In ext-base.js file
they wrap the all code inside:

(function() {
...
...
...

)()

What is that mean?
( expression )( parameters )

Evaluate "expression" and (try to) call the result as a function with
the parameters "parameters".

"expression" does not need to be a "literal function" expression.

for example :

var p= "Hola";
var f= function (p) { alert(p) };
var g= function () { return f };
var h= [f, g];

(f)(p);
(g())(p);
(h[0])(p);
(h[1]())(p);
(window.alert)(p);
(function (q) { alert(q) })(p);

http://tinyurl.com/68ffac

--Jorge.
Jul 7 '08 #4

P: n/a
On Jul 7, 3:47*pm, Jorge <jo...@jorgechamorro.comwrote:
On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.comwrote:


Hi All
When I looked at ExtJS source code, I found something strange.
could anyone here explain to me?
In ext-base.js file
they wrap the all code inside:
(function() {
...
...
...
)()
What is that mean?

( expression )( parameters )

Evaluate "expression" and (try to) call the result as a function with
the parameters "parameters".

"expression" does not need to be a "literal function" expression.

for example :

var p= "Hola";
var f= function (p) { alert(p) };
var g= function () { return f };
var h= [f, g];

(f)(p);
(g())(p);
(h[0])(p);
(h[1]())(p);
(window.alert)(p);
(function (q) { alert(q) })(p);

http://tinyurl.com/68ffac
Note that an assignment has a result as well :

var i;

(i= f)(p);
((i= h[1])())(p);

--Jorge.
Jul 7 '08 #5

P: n/a
On Jul 7, 3:47*pm, Jorge <jo...@jorgechamorro.comwrote:
On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.comwrote:


Hi All
When I looked at ExtJS source code, I found something strange.
could anyone here explain to me?
In ext-base.js file
they wrap the all code inside:
(function() {
...
...
...
)()
What is that mean?

( expression )( parameters )

Evaluate "expression" and (try to) call the result as a function with
the parameters "parameters".

"expression" does not need to be a "literal function" expression.

for example :

var p= "Hola";
var f= function (p) { alert(p) };
var g= function () { return f };
var h= [f, g];

(f)(p);
(g())(p);
(h[0])(p);
(h[1]())(p);
(window.alert)(p);
(function (q) { alert(q) })(p);

http://tinyurl.com/68ffac
Note that an assignment yields a result as well :

var i, j;

(i= f)(p);
(j= (i= h[1])())(p);
(j)(p);
(i())(p);

--Jorge.
Jul 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.