JS Constructor Design Pattern
Useful!
by Steven Senkus
HTML
<div>JS Constructor Practice</div>
JavaScript
function scriptTag(src, id, nickname, ready) {
this.type = 'text/javascript';
this.id = id;
this.src = src;
this.nickname = nickname;
this.ready = ready;
this.insert = function () {
var s = document.createElement('script');
s.src = window.location.protocol + this.src;
s.type = this.type;
s.id = this.id;
s.asyc = true;
var that = this;
document.getElementsByTagName("head")[0].appendChild(s);
s.onload = s.onreadystatechange = function () {
console.log(that.nickname + ' is ready');
that.ready();
};
return 'Added:' + this.nickname;
};
}
var jquery_script = new scriptTag('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', 'jq', 'jQuery-1.10.2', function () {
$(document).ready(function () {
$('div').fadeOut(1000, function () {
$('body').css({
'background-color': '#f00',
'font-size': '50px',
'text-align': 'center',
'font-family': 'Helvetica'
});
$('div').fadeIn(2000);
});
})
});
console.log(jquery_script.insert());