Created
June 30, 2012 09:02
-
-
Save gja/3023040 to your computer and use it in GitHub Desktop.
Eight Simple Rules of Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(i = 0; i < 5; i++) { | |
var times = i; // need to save this in our closure | |
String.prototype["alert" + i] = function() { | |
alert(this + times); | |
} | |
}; | |
"foobar".alert3() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.alert = function() { | |
alert(this); | |
}; | |
"foobar".alert(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alertFunction = this["alert"]; | |
alertFunction("blahblahblah"); // Note that alert will no longer get the same value of this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="submit-button">Submit</div> | |
<script type="text/javascript"> | |
bindElementToSubmit($(".submit-button")); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bindElementToSubmit(element) { | |
element.click(function() { | |
server.submit(); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("some-div").click(function() { | |
var self = this; | |
self.html("loading"); | |
$.get("/foobar", function(result){ | |
self.html(result); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SubmitPage = Class.extend({ | |
processResults: function(results) { | |
// do Something | |
}, | |
constructor: function(element) { | |
var self = this; | |
element.click(function(){ | |
server.submit(function(result){ | |
self.processResults(results); | |
}); | |
}); | |
} | |
}); | |
new SubmitPage($(".submit-button")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(".submit-button").click(function(){ | |
server.submit(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("some-div").click(function() { | |
this.html("loading"); // replaces the current div with 'loading' | |
$.get("/foobar", function(result){ | |
this.html(result); // no such function html!! | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function processResults(results) { | |
// do Something | |
} | |
function bindElementToSubmit(element) { | |
element.click(function(){ | |
server.submit(function(result){ | |
processResults(results); | |
}); | |
}); | |
} | |
bindElementToSubmit($(".submit-button")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment