The Artima Developer Community
Sponsored Link

Python Buzz Forum
Beware of javascript's global scope

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Carlos de la Guardia

Posts: 219
Nickname: cguardia
Registered: Jan, 2006

Carlos de la Guardia is an independent web developer in Mexico
Beware of javascript's global scope Posted: Jun 5, 2006 11:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Carlos de la Guardia.
Original Post: Beware of javascript's global scope
Feed Title: I blog therefore I am
Feed URL: http://blog.delaguardia.com.mx/feed.atom
Feed Description: A space to put my thoughts into writing.
Latest Python Buzz Posts
Latest Python Buzz Posts by Carlos de la Guardia
Latest Posts From I blog therefore I am

Advertisement

When trying to port some code from Python to Javascript I started to get unexpected results from a simple recursive function.

Here is the Python code:

def childrenMarkup(children):
markup=''
for child in children:
markup ='<div dojoType="TreeNode"
widgetId="'+child.id+'"
title="'+child.title+'">'+
childrenMarkup(child.children)+'</div>'
return markup

My first javascript attempt looked like this:

function childrenMarkup(children) {
markup='';
if (children.length==0) { return markup; }
for (var i=0;i<children.length;i ++ ) {
var child=children[i];
markup ='<div dojoType="TreeNode" widgetId="'+
child.widgetId+'" title="'+child.title+'" object="'+
child.object+'">'+childrenMarkup(child.children)+'</div>';
}
return markup
}

This caused the script to return strange values that did not reflect the tree structure I wanted to convert to HTML.

Python is very similar in many respects to Javascript, so I thought a semi-direct translation would work. Of course, you may already know that I forgot a key Javascript scoping rule, which is almost exactly the opposite from Python's: all variables are global unless declared with the var keyword.

That means my children array and for loop variable got squashed with each call, and the way it happened depended on the composition of the tree, hence the unconsistent results.

Halfway through my suffering I remembered reading this somewhere before, so after correcting my work I looked for it. Sure enough , I found it right away on Mozilla's Javascript reference. My final version took this into account:

function childrenMarkup(children) {
var branches=children;
var markup="";
if (branches.length==0) { return markup; }
for (var i=0;i<branches.length;i ) {
var child=branches[i];
markup ='<div dojoType="TreeNode" widgetId="'+
child.widgetId+'" title="'+child.title+'" object="'+
child.object+'">'+childrenMarkup(child.children)+'</div>';
}
return markup
}

Read: Beware of javascript&#039;s global scope

Topic: TurboGears Identity testing Previous Topic   Next Topic Topic: What is Simplicity?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use