Created
March 13, 2014 12:51
-
-
Save iladarsda/9527884 to your computer and use it in GitHub Desktop.
JavaScript - method chaining
This file contains 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
$.fn.redBorder = function () { | |
$(this).css("border", "1px solid red"); | |
return this; | |
// or shorter "return $(this).css("border", "1px solid red");" | |
}; | |
$.fn.greyBackground = function () { | |
return $(this).css("background-color", "grey"); | |
}; | |
$.fn.innTxt = function (txt) { | |
return $(this).html(txt); | |
}; | |
$('#main').redBorder().greyBackground().innTxt("testing chaining"); |
This file contains 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
var value = 0; | |
var obj = { | |
increase: function(val) { | |
value = value + val; | |
return obj; | |
}, | |
decrease: function(val) { | |
value = value - val; | |
return obj; | |
} | |
} | |
obj.increase(5).increase(2).decrease(1); | |
console.log(value); // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment