Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 10, 2015 19:04
Show Gist options
  • Save mayfer/1bb2062374df565b286b to your computer and use it in GitHub Desktop.
Save mayfer/1bb2062374df565b286b to your computer and use it in GitHub Desktop.
Example showing methods within instances of functions, via the "this" keyword. Also, chaining methods by returning "this" every time.
<html>
<head>
<style>
body { background: #358; color: #fff; padding: 50px; font-family: sans-serif; }
</style>
</head>
<body>
<script>
var methodyObject = function() {
this.my_method = function() {
console.log("YAY a method");
return this;
}
this.other_method = function() {
console.log("omg YAY other");
}
}
var obj = new methodyObject();
var result = obj.my_method().other_method();
console.log(result);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment