Posted by: ทวี | มีนาคม 19, 2007

Object Oriented Programming Using JavaScript

Object Creation :

<script language=”javascript” contenttype=”text/javascript”>
<!–
// object creation using ‘new Object()’
person = new Object()
person.name = “Tim Scarfe”
person.height = “6Ft”
person.run = function() {
this.state = “running”
this.speed = “4ms^-1″
}

// Object creation using Literals
timObject = {
property1 : “Hello”,
property2 : “HmmMMm”,
property3 : ["mmm", 2, 3, 6, "kkk"],
method1 : function() {
document.write(“Method had been called ” + this.property1)
}
}

// Object constructor and prototyping
function cat(name) {
this.name = name;
this.talk = function() {
document.write(this.name + ” say meeow!”)
}
}

// Adding methods to our object using prototype
cat.prototype.changeName = function(newName) {
this.name = newName;
}
//–>
</script>


Leave a response

Your response:

หมวดหมู่