Is this a closure?

D

donarb

About a week ago I was on a web page (which I can't find now) that had
some tricky javascript. I remember that some of the top-level object
definitions were surrounded by braces, I assume they were closures, but
I can't find any information online about this particular syntax. It
reminds me of something in Damien Conway's OO Perl book, what he calls
a shared lexical. Is it a closure and what would the benefit be?


This is a sample of what I recall it looked like:

{
function myObject() {}
myObject.prototype.dosomething = function() {}
myObject.prototype.dosomethingelse = function() {}
}
 
R

Richard Cornford

About a week ago I was on a web page (which I can't find now) that had
some tricky javascript. I remember that some of the top-level object
definitions were surrounded by braces, I assume they were closures, but
I can't find any information online about this particular syntax. It
reminds me of something in Damien Conway's OO Perl book, what he calls
a shared lexical. Is it a closure and what would the benefit be?

This is a sample of what I recall it looked like:

{
function myObject() {}
myObject.prototype.dosomething = function() {}
myObject.prototype.dosomethingelse = function() {}
}

It would be better if you could post what it actually looked like
rather than what you recall. There are no closures formed in this code.
It is an ordinarily looking definition of a constructor with
assignments to its - prototype - property, but unexpectedly (and
pointlessly) surrounded by a Block Statement.

Richard.
 
V

VK

About a week ago I was on a web page (which I can't find now) that had
some tricky javascript. I remember that some of the top-level object
definitions were surrounded by braces, I assume they were closures, but
I can't find any information online about this particular syntax. It
reminds me of something in Damien Conway's OO Perl book, what he calls
a shared lexical. Is it a closure and what would the benefit be?

While the recall you provided doesn't look right, my best guess would
be that you saw a code using CC (Cornford-Crockford) scope management.
On the first glance it looks as weird as you tried to depict :)

Take a look at
<http://www.crockford.com/javascript/private.html>
and especially at
<http://www.litotes.demon.co.uk/js_info/private_static.html>
if it reminds you something.
 

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

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top