Question on Object oriented Programming in JS

Q

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
 
S

Stevo

QQ said:
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.
 
J

Joost Diepenmaat

QQ said:
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.
 
J

Jorge

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

Jorge

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

Jorge

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.
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top