<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
// 工厂函数 创建多个对象
// function createHero(name, blood, weapon) {
// var o = new Object();
// o.name = name;
// o.blood = blood;
// o.weapon = weapon;
// o.attack = function () {
// console.log(this.weapon + ' 攻击敌人');
// }
// return o;
// }
// 构造函数 -- 构造对象的函数
function Hero(name, blood, weapon) {
this.name = name;
this.blood = blood;
this.weapon = weapon;
this.attack = function () {
console.log(this.weapon + ' 攻击敌人');
}
}
// 1 会在内存中创建一个空对象
// 2 设置构造函数的this,让this指向刚刚创建好的对象
// 3 执行构造函数中的代码
// 4 返回对象
var hero = new Hero('刘备', 100, '剑');
// hero.attack();
//
// 无法使用typeof获取对象的具体类型
// console.log(typeof hero);
// var arr = new Array();
// console.log(typeof arr);
// constructor 构造器 / 构造函数
// 获取对象的具体类型 不建议
// console.log(hero.constructor === Hero);
// var arr = []; //new Array()
// console.log(arr.constructor === Array);
//
//
// instanceof 判断某个对象是否是某个构造函数的实例/对象
console.log(hero instanceof Hero);
var arr = [];
console.log(arr instanceof Array);
</script>
</body>
</html>