Skip to content

Instantly share code, notes, and snippets.

@torus
Created November 2, 2010 02:35
Show Gist options
  • Save torus/659186 to your computer and use it in GitHub Desktop.
Save torus/659186 to your computer and use it in GitHub Desktop.
Comparing with structure and Function.call method.
<html>
<head>
<title>with vs Function.call</title>
</head>
<body>
<p>See JavaScript console using Chrome developer tool or Firebug.</p>
<script id="script">
console.log ("using with...");
var a = {value_a: "a"};
var b = {value_b: "b"};
with (a) {
with (b) {
console.log (this);
console.log (value_a);
console.log (value_b);
}
}
console.log ("using Function.call...");
(function () {
console.log (this);
console.log (this.value_a);
console.log (value_a); // Error on Chrome
}).call (a)
</script>
<h2>Source</h2>
<script>
var e = document.createElement ("pre");
e.textContent = document.getElementById ("script").innerHTML;
document.body.appendChild (e);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment