I am trying to build my own objects with methods in JavaScript however, failed at my first ever try with the following code returning the complete code along with " has no method 'writeOut' in the chrome dev tools console.
var link = function bhLink(options) {
defaultOptions = {
targetURL: '#',
target: '_blank',
textColor: '#000',
bgColor: '#fff',
font: 'Arial',
fontSize: '12px',
lineHeight: '12px',
text: '[Test]'
}
if (typeof options == 'object') {
options = $.extend(defaultOptions, options);
} else {
options = defaultOptions;
}
link.prototype.writeOut = function() {
return $('<a></a>')
.prop({'href':this.targetURL, 'target': this.target})
.css({'font-family':this.font, 'color':this.textColor, 'font-size': this.fontSize, 'line-height':this.lineHeight});
}
} // end link
I use it like
$('#id_of_some_button').click(function(e) {
e.preventDefault();
$('#id_of_some_div').html(link.writeOut);
});
Both the click event and the link is within $(document).ready({}); blocks.
Any ideas?
EDIT ==============================================
Added options as argument and an if statement to check wether there are options provided..
link? if not, then it hasn't been giving the writeOut method yet. Usually methods are added to the prototype outside of the constructor.link.writeOutaccesses a property, whilelink.writeOut()executes a method.