LISTSERV mailing list manager LISTSERV 16.5

Help for CSSA Archives


CSSA Archives

CSSA Archives


CSSA@LIST.UVM.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

CSSA Home

CSSA Home

CSSA  October 2009

CSSA October 2009

Subject:

Re: On Scalable Programming Languages

From:

Nick Husher <[log in to unmask]>

Reply-To:

Computer Science Student Association <[log in to unmask]>

Date:

Mon, 26 Oct 2009 14:48:45 -0400

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (93 lines)

> We could always just replace block open and close.
>
> block open with:
> (function() {
>
> block close with:
> }());
>
> would look like this:
> if (something) (function() {
> 	alert('it's ugly-time');
> }());
>
> Unfortunately, javascript's function literal isn't as awesome as its  
> object literal.

Yeah, that's what I meant by 'creating a new function'. In general,  
unless you're serious about data encapsulation, you'll only rarely  
need to do such a thing. It's also possible to be overzealous about  
creating anonymous function blocks: every function block carries with  
it a lot of complex operating context information that can have a  
performance impact (dynamically creating 1000 new closures with unique  
scopes available to them will cause slower browsers and slower  
machines to die cruelly).

Were I teaching someone about Javascript in an academic environment, I  
would probably spend a lot longer on scope peculiarities with  
Javascript than I would with any other language. In one sense, it's  
more intuitive because there's no "masking" of more-global variables*,  
but it's a lot more confusing because functions are lexically scoped  
rather than dynamically scoped**.

If I remember correctly, there was a proposal in Javascript 4 (ECMA6)  
that introduced a simple syntactical way to define a new context  
without creating a new function. I'm fairly sure it was just sugar for  
the code you wrote above, but it would make such code far more  
readable. Unfortunately, JS4 is dead for the time being. Most work on  
JS these days is in performance enhancements and feature extensions  
(see canvas, offline storage, etc) and there's a legitimate argument  
that any syntactic changes to JS will "break the web," an eventuality  
which has become intolerable as the web has matured.

I'm going to guess that Jacob knows the following, so this is aimed  
more at the casual reader interested in learning about Javascript:

* In C-like languages (C++, Java, etc) we can observe the following  
behavior. It's unintuitive to assign 10 to an already-existing  
variable and have it "mask" it rather than perform an actual  
assignment. Removing the "int" changes the behavior in a subtle but  
very important way. Using this property of the language is really  
powerful, but can lead to logic errors pretty quickly if you aren't  
careful.

In javascript, we can write the equivalent and observe the difference.  
Assignments that take place within blocks (even if you attempt to  
redefine the variable) affect variables of the same name outside the  
block.

// C-like language:
int i = 5;
if(1==1) {
	int i = 10;
	printf("%d\n", i); // prints "10"
}
printf("%d\n", i); // prints "5"

// Javascript
var i = 5;
if(true) {
	var i = 10;
	console.log(i); // prints "10"
}
console.log(i); // prints "10"


** Lexical scoping is probably one of the trickiest aspects of  
Javascript that took me a long time to really internalize and use  
properly. It allows for the creation of closures, which are one way to  
do data hiding and, with Javascript's built-in scope binding  
functions, allow you to bend a function's scope to operate in many  
contexts. A basic example:

var push, pop;
(function() {
	var array = [];
	push = function(el) { array.push(el); };
	pop = function() { return array.pop(); };
}());
push("STRING");

alert(pop()); // alerts "STRING"
alert(array) // alerts "undefined";

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

February 2020
July 2019
March 2019
September 2018
June 2018
August 2017
July 2017
June 2017
October 2016
September 2016
August 2016
July 2016
June 2016
April 2016
October 2012
August 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
May 2007
April 2007
March 2007
February 2007
January 2007
November 2006
October 2006
September 2006
June 2006
May 2006
April 2006
March 2006
January 2006
December 2005
November 2005
October 2005
September 2005
July 2005
May 2005
April 2005
March 2005
February 2005
January 2005
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
May 2002
April 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
July 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
June 2000
May 2000
April 2000
February 2000
January 2000
November 1999
October 1999
September 1999
July 1999
April 1999
March 1999
January 1999
November 1998
October 1998
September 1998
August 1998
July 1998
April 1998
March 1998

ATOM RSS1 RSS2



LIST.UVM.EDU

CataList Email List Search Powered by the LISTSERV Email List Manager