Monday, May 25, 2009

use (function(){})() to do the scope thing.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<input type="button" id="b" value="click me" />
<script type="text/javascript">
function g(){
console.log('g0');
}

(function(){
function g(){
console.log('g1');
}
document.getElementById('b').onclick = g;
})();

(function(){
function g(){
console.log('g2');
}
g();
})();
</script>
</body>
</html>

No comments:

Post a Comment