Created
March 11, 2012 13:31
-
-
Save brianyang/2016479 to your computer and use it in GitHub Desktop.
javascript objects
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
var Class = function(parent){ | |
var klass = function(){ | |
this.init.apply(tthis,arguments) | |
} | |
klass.prototype.init = function(){} | |
klass.fn = klass.prototype | |
klass.proxy = function(func){ | |
var self = this | |
return (function(){ | |
return func.apply(self,arguments) | |
}) | |
} | |
klass.fn.proxy = klass.proxy | |
return klass | |
} | |
var Button = new Class | |
Button.include({ | |
init: function(element){ | |
this.element = jQuery(element) | |
// proxy the click function | |
this.element.click(this.proxy(this.click)) | |
}, | |
click: function(){ /* -- */ } | |
}) |
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
$('.clicky').click(function(){ | |
$(this).hide() | |
}) | |
$('p').each(function(){ | |
$(this).remove() | |
}) | |
var clicky = { | |
wasClicked: function(){ | |
}, | |
addListeners: function(){ | |
var self = this | |
$('.clicky').click(function(){ | |
self.wasClicked() | |
}) | |
} | |
} | |
clicky.addListeners() | |
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
var proxy = function(func,thisObj){ | |
return(function(){ | |
return func.apply(thisObj,arguments) | |
}) | |
} | |
var clicky = { | |
wasClicked: function(){ | |
/* -- */ | |
}, | |
addListeners: function(){ | |
var self = this | |
$('.clicky').click(proxy(this.wasClicked,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
$('.clicky').click($.proxy(function(){ /* -- */ },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
// delegating calls after passing arguments | |
var App = { | |
log: function(){ | |
if (typeof console == "undefined") return | |
var args = jQuery.makeArray(arguments) | |
args.unshift("(App)") | |
console.log.apply(console,args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment