Created
November 2, 2010 12:44
-
-
Save torjusb/659559 to your computer and use it in GitHub Desktop.
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
// Create an inherhit / extend method | |
Function.prototype.inherits = function (class) { | |
$.extend(this.prototype, class.prototype); | |
} | |
var ExtendClass = function () { | |
// Call the "parent" constructor | |
Foobar.apply(this); | |
// Parent method | |
this.foo(); | |
} | |
// Extended class' method | |
ExtendClass.prototype.superMethod = function () { | |
console.log('cake'); | |
return this; | |
} | |
// Extend the Foobar class with the ExtendClass | |
ExtendClass.inherits(Foobar); | |
// Make it global | |
window.ExtendClass = ExtendClass; |
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
( function (window, $) { | |
/** | |
** @ Constructor | |
** | |
** Function constructors should _always_ be capitalized | |
**/ | |
var Foobar = function () { | |
// Properties | |
this.foop = 'bar'; | |
}; | |
/** | |
** @ Methods | |
**/ | |
Foobar.prototype = (function () { | |
// Private methods | |
var privateFoo = function () { | |
console.log('private stuff'); | |
}, | |
privateBaz = function () { | |
return this; | |
} | |
// Public methods | |
return { | |
foo: function () { | |
// Private methods should be called using the apply method | |
privateFoo.apply(this); | |
return this; | |
}, | |
baz: function () { | |
return this; | |
} | |
} | |
})(); | |
/** | |
** @ Static methods | |
**/ | |
Foobar.staticFoo = function () { | |
console.log('static foo'); | |
return this; | |
} | |
Foobar.staticBaz = function () { | |
console.log('static baz'); | |
return this; | |
} | |
/** | |
** @ Static properties | |
**/ | |
Foobar.defaults = { | |
'foo': 'bar', | |
'baz': 'foo' | |
} | |
// Make it global | |
window.Foobar = Foobar; | |
/** | |
** @ jQuery plugin | |
**/ | |
$.fn.foobar = function () { | |
return this.each( function () { | |
return new Foobar(); | |
}); | |
}; | |
})(window, jQuery, undefined); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment