<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
//通过new Object创建对象
var hero =new Object();
hero.name="盖伦";
hero.kill=function(){
document.write(this.name+"正在杀敌。。。<br>")
}
hero.kill();
//通过自定义方法创建对象
function Hero(name){
this.name=name;
this.kill=function(){
document.write(this.name+"正在杀敌<br>")
}
}
//使用原型来对已经存在的对象添加方法或者属性,增加了拓展性
Hero.prototype.keng=function(){
document.write(this.name+"正在坑队友。。<br>")
}
var timo=new Hero("提莫");
timo.kill();
timo.keng();
var gailun=new Hero("盖伦");
gailun.kill();
gailun.keng();
</script>
</html>
js创建对象的两种方法(new Object和自定义方法)
最新推荐文章于 2022-09-06 11:03:34 发布