i have just started to making class in javascript, this is my first class that i have created something like:
function MessageBox(parent){
this.id = "msg-box"; // ID for the Box
this.createElements(this.id);
this.parentElement = parent;
};
MessageBox.prototype = {
createElements: function(id){
// Create Main Container
this.containerElement = document.createElement('div');
this.containerElement.setAttribute('id', this.id);
this.parentElement.appendChild(this.containerElement);
}
};
but when i call this file in my index.html file it returns the error createElements() is not a function..
here is my index.html as well:
<script type="text/javascript">
window.onload = function(){
var box = MessageBox('body');
//box.start();
}
</script>
As your function is a constructor function, you want to use
Else, no instance is created and your constructor is executed on the default context object, the global
windowobject (which has nocreateElementsproperty). Have a look at MDN’s introduction to thethiskeyword and thenewoperator.