JAVASCRIPT 面向对象入门 示例

<html >
<head>
<title></title>

</head>

<body>




<script type="text/javascript">
/*
person class
*/
function Person(name){
this.name = name;
}

Person.prototype ={
getName : function(){
return this.name;
},
setName : function(name){
this.name = name;
},
showName : function(){
alert(this.name);
}

}

var xm = new Person("xiaoming");
xm.showName();
xm.setName("zhangsan");
xm.showName();


/*
extend class(dynamic)
*/
Person.prototype.getGreeting = function(){
return "hello," + this.name;
}

alert(xm.getGreeting());

/*

*/

</script>


</body>
</html>


你可能感兴趣的:(JAVASCRIPT 面向对象入门 示例)