Skip to content

Instantly share code, notes, and snippets.

@gja
Created June 30, 2012 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gja/3023040 to your computer and use it in GitHub Desktop.
Save gja/3023040 to your computer and use it in GitHub Desktop.
Eight Simple Rules of Javascript
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()
String.prototype.alert = function() {
alert(this);
};
"foobar".alert();
alertFunction = this["alert"];
alertFunction("blahblahblah"); // Note that alert will no longer get the same value of this
<div class="submit-button">Submit</div>
<script type="text/javascript">
bindElementToSubmit($(".submit-button"));
</script>
function bindElementToSubmit(element) {
element.click(function() {
server.submit();
});
}
$("some-div").click(function() {
var self = this;
self.html("loading");
$.get("/foobar", function(result){
self.html(result);
});
});
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"));
$(".submit-button").click(function(){
server.submit();
});
$("some-div").click(function() {
this.html("loading"); // replaces the current div with 'loading'
$.get("/foobar", function(result){
this.html(result); // no such function html!!
});
});
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