Skip to content

Instantly share code, notes, and snippets.

@brianyang
Created March 11, 2012 13:31
Show Gist options
  • Save brianyang/2016479 to your computer and use it in GitHub Desktop.
Save brianyang/2016479 to your computer and use it in GitHub Desktop.
javascript objects
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(){ /* -- */ }
})
$('.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()
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))
}
}
$('.clicky').click($.proxy(function(){ /* -- */ },this))
// 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