The type attribute is required.
Code:
function person()
...
James = new person();
The convention for constructor functions is that they start with a capital, and the convention for variables is that they don't. In English we use capitals for proper nouns; in Javascript, we don't.
This is what prototypes are for.
Code:
setTimeout("this.walk()",5000)
If you pass a string to setTimeout() (which is bad practice), it will be evaluated in the global scope, so this isn't defined. Instead, you can store a reference to the current object in a variable and use a closure:
Code:
<script type="text/javascript">
function Person() {
this.move =
this.position =
0;
}
Person.prototype.walk = function() {
this.position += 2;
if(this.position <= 10) {
alert("Walking");
var me = this;
setTimeout(function() { me.walk(); }, 5000);
} else
alert("Finished walking")
};
var james = new Person();
james.walk();
</script>
Bookmarks