I’m having trouble with javascript’s oop. I have a small code snippet which is similar to the code I’m using in my project. I wrote a small code example, I’m having trouble with this too. I’d like to know what I’m doing wrong with my Javascript, so my code will work.
function Person(name) {
this.name = name;
}
Person.prototype.Display = {};
Display.prototype.text = function(str) {
document.write(str + '<br />');
window.alert(str);
};
var Jacob = new Person('Jacob');
Jacob.Display.text('Hello World!');
This small code example is supposed to display the text hello world. The trouble I’m having is that the line "Jacob.Display.Text('Hello World!');" is not working.
I think you mean this:
(Also note the lowercase ‘d’ in ‘display’; leave uppercase initials for constructors)