473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

About (function(){})()

It has been widely used nowadays to wrap JavaScript libraries, as seen
in JQuery.

(function() {
var global = this;
// ...
})();

The advantage, as I learned, is the isolation from other global
variables. But how? What other advantages does this wrapper have. For
example, I have my library as:

var myLib = {
go : function(){
alert('Hah')
},
arr : new Array()
}

How would I write myLib in (function(){})() format? Do I need to? What
are the advantages?
Please clear for me if you can.
Thanks
Oct 29 '08 #1
3 1494
On Oct 29, 9:46*am, vunet <vunet...@gmail.comwrote:
It has been widely used nowadays to wrap JavaScript libraries, as seen
in JQuery.
Seen long before jQuery.
>
(function() {
* var global = this;
* // ...

})();
Actually, that looks like my library.
>
The advantage, as I learned, is the isolation from other global
variables. But how? What other advantages does this wrapper have. For
example, I have my library as:
How what? As for other advantages, "minifiers" can be more aggressive
with this pattern.
>
var myLib = {
* go : function(){
* * alert('Hah')
* },
* arr : new Array()

}

How would I write myLib in (function(){})() format? Do I need to? What
It is hard to say what you need to do. The sample above doesn't
indicate what you are trying to do.
are the advantages?
For one, you can have protected members, which you cannot do with an
object literal alone.

[snip]
Oct 30 '08 #2
hj
On Oct 29, 6:46 am, vunet <vunet...@gmail.comwrote:
It has been widely used nowadays to wrap JavaScript libraries, as seen
in JQuery.

(function() {
var global = this;
// ...

})();

The advantage, as I learned, is the isolation from other global
variables. But how? What other advantages does this wrapper have. For
example, I have my library as:
Here's a concrete example: Imagine that you have some functions to
declare, *and*
some code that you want to be executed, e.g.:
function x() {
// Do something
}
function y(a) {
// Do something else
}
function untilSometime() {
x();
if ((now.getTime() + 1000) < (new Date().getTime())) {
y();
} else {
clearInterval(ihandle);
}
}
var now = new Date;
var ihandle = setInterval(untilSometime,100);
The result is that "x", "y", "untilSometime", "now", and "ihandle" are
all
global properties. There's a possibility that any of them have
overwritten
pre-existing properties with the same names, as well as the
possibility that
they'll be overwritten by code later in the document. Wrapping the
whole
thing in an immediately executed function expression makes all those
local
to that function, and avoids both those problems; and produces the
desired
effect without creating -any- global properties.

--

hj
Oct 30 '08 #3
On Fri, 31 Oct 2008 07:18:02 +0100, RobG <rg***@iinet.net.auwrote:
On Oct 30, 11:55 pm, vunet <vunet...@gmail.comwrote:
[...]
>Thank you for very descriptive answer. Can I possible clear out about
what happens when this is excecuted:

(function() {
// declare myLib
var myLib = {...};

// now use it.
myLib.go(...);
// and so on

})();

The code is executed on JS run-time and is hidden to any other
libraries or variables because of the wrapper function. But at the
same time that code cannot be accessed from outside if I have onClick
handler to run one of the functions inside like onClick="myLib.go()".
Is this more "robust" (as you say) because it is global:
var global = (function(){return this})();

No. I suggested it because that method of getting a reference to the
global object can be used anywhere, you don’t have to run it as global
code.

Using an anonymous function wrapper around your library is not
necessarily more robust (depending on what you think that means), in
fact it can make life more difficult as you have to add listeners
dynamically, which introduces a whole new set of issues. If it is
purely to avoid naming clashes, an good technique is to use a prefix
for your functions, e.g.

function vU_go() { ... }
function vU_stop() { ... }

And so on. That method provides about the same protection as the
object:property method of:

var vU = {
go: function() {...}
stop: function() {...}
}

If you can get away with simple listeners like:

<input type=”button” onclick=”vU_go()”>
Hmmm, and prevent 'smart' applications from converting your carefully used
'"'s to '”'....

(=94 =decimal 148 in the windows-1252/quoted-printable source)

/prays to a random deity for this post to make sense when sent, I don't
even trust my nntp client all that much...
--
Rik
Nov 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ryan | last post by:
My project uses the /ORDER specifier to order functions as specified in file containing the decorated names of packaged functions (COMDATs). I am in the process of upgrading from VC6 to VC.NET 2003....
3
by: amitmehrotra007 | last post by:
I have an old fortran library which has a function f1 which expects a function pointer to be passed to it of the type double(*) (const double &) I have a class Foo which has two functions class...
3
by: neilcancer | last post by:
My English is poor... There are some sort algorithms which sort a sequencial list. The sequencial list was defined in "list-seq.h". sort_and_time() accepts one of these sort functions. It uses...
9
by: key9 | last post by:
Hi all My question is why the code CAN pass the compiler , and all the resoult is right? I means in my mind the reffoo(3,*test); should not pass compiler. #include <stdio.h>
3
by: Ara Kooser | last post by:
Hello all, I hope I am posting this correctly. I am running Python 2.4.2 on Slackware 11.0 using IDLE. I am in the process of learning python so I am writing a text adventure game using...
3
by: alex | last post by:
<script> var sd=function(){ return{ f1:function(){ alert('f1'); }, f2:function(){ alert('f2'); } }
1
by: Diogenes | last post by:
Hi all, I had a look at sIFR.js code after parsing it and I have a question about function syntax. For example var f=function(){ // statements here }();
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
2
by: Oltmans | last post by:
Hi all, I'm stuck in a situation where I need help. Any help will be highly appreciated. I've created an object, after creating the object, I'm assigning one of its functions to an event handler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.