CSS & Web Design Forum  

Welcome to the CSS & Web Design Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
WM Media Banner

Supporters
Pos Software - POS hardware and Software to track, control and manage your inventory.
Web Templates - BoxedArt is the most reliable source for unlimited template downloads.

Directory Submission
WL Marketing - Your #1 source for building links

Go Back   CSS & Web Design Forum > Web Design > AJAX, Javascript & DOM
Reply
 
Thread Tools Search this Thread Display Modes
(#1)
Old
QQ
Guest
 
Default Question on Object oriented Programming in JS - 07-07-2008, 11:14 AM

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
Reply With Quote
(#2)
Old
Stevo
Guest
 
Default Re: Question on Object oriented Programming in JS - 07-07-2008, 11:52 AM

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.
Reply With Quote
(#3)
Old
Joost Diepenmaat
Guest
 
Default Re: Question on Object oriented Programming in JS - 07-07-2008, 12:12 PM

QQ <chaojiang.au@gmail.com> writes:

> 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/
Reply With Quote
(#4)
Old
Jorge
Guest
 
Default Re: Question on Object oriented Programming in JS - 07-07-2008, 01:53 PM

On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.com> wrote:
> 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.
Reply With Quote
(#5)
Old
Jorge
Guest
 
Default Re: Question on Object oriented Programming in JS - 07-07-2008, 02:16 PM

On Jul 7, 3:47*pm, Jorge <jo...@jorgechamorro.com> wrote:
> On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.com> wrote:
>
>
>
>
>
> > 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.
Reply With Quote
(#6)
Old
Jorge
Guest
 
Default Re: Question on Object oriented Programming in JS - 07-07-2008, 03:29 PM

On Jul 7, 3:47*pm, Jorge <jo...@jorgechamorro.com> wrote:
> On Jul 7, 1:00*pm, QQ <chaojiang...@gmail.com> wrote:
>
>
>
>
>
> > 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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Hosting at Triple.com
vBulletin Skin developed by: vBStyles.com

Printing Company  Link Building Services