<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 构造函数创建对象
function Hero(name, skill, hp, mp, adress, home, price) {
this.name = name;
this.skill = skill;
this.hp = hp;
this.mp = mp;
this.adress = adress;
this.home = home;
this.price = price;
var a = 78;
// this.sayName = function () {
// alert(this.name); }
// 函数修改
this.sayName = sayName;
}
var sayName = function () {
console.log(this.name); //问题:::::::this为何能在外面生效??????????????????????????????????????????????
}
// 上述命名的对象只是一类事物所公共拥有的特征
var gailun = new Hero('盖伦', '德玛西亚', 7000, 0, '英雄联盟', '德玛西亚之都', 6300);
for (var key in gailun) {
console.log(gailun[key]);
}
// 操作对象
gailun.sayName();
var lianjin = new Hero('炼金术师', '祖安', 7800, 90000, '英雄联盟', '祖安支援者', 1350);
console.log('--------------------');
for (var key in lianjin) {
console.log(key) //循环遍历属性名,z这为什么没有打印下面的speed????????????????????????????????????
}
for (var key in lianjin) {
console.log(lianjin[key]); //循环遍历属性值
}
for (var key in lianjin) {
console.log(key + ':' + lianjin[key]); //遍历属性名加值得组合
}
// =================================================================
lianjin['price'] = 8700; // 更改属性值
lianjin['speed'] = '1788km/h'; //增加属性
console.log(lianjin['speed']); //后增加的属性并没有被之前对象循环遍历的打印上去?????????????????????????????????
for (var key in lianjin) {
console.log(key);
}
</script>
</body>
</html>