原型prototype
共⽤⽅法(⾏为)都写在原型上
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编写第⼀个⾯向对象程序之⼯⼚函数(⼆)</title>
</head>
<body>
<script type="text/javascript">
//构造函数
function createMan(name1,age1){
//创建对象
//var obj = new Object()
//属性
this.name = name1
this.age = age1
}
//利用原型来添加共用的方法
createMan.prototype.showName=function(){
alert('我的名字是:'+this.name)
}
createMan.prototype.showAge=function(){
alert('我的名字 是:'+this.age)
}
//使用new关键字
var obj1 = new createMan('tim',18)
var obj2 = new createMan('tom',20)
obj1.showName()
obj2.showName()
alert(obj1.showName==obj2.showName) //返回true
</script>
</body>
</html>