参见: https://zhuanlan.zhihu.com/p/23987456
注: 文章中提到士兵.原型 = {} 的原因是因为如果单独写: 原型={}, 这个对象看起来会和 士兵={}没有明显的联系, 所以将共有属性以士兵.原型 = {} 的方式存储, 看起来关联紧密
var 士兵 = {
ID: 1, // 用于区分每个士兵
兵种:"美国大兵",
攻击力:5,
生命值:42,
行走:function(){ /*走俩步的代码*/},
奔跑:function(){ /*狂奔的代码*/ },
死亡:function(){ /*Go die*/ },
攻击:function(){ /*糊他熊脸*/ },
防御:function(){ /*护脸*/ }
}
兵营.create(士兵)
// 100
var 士兵们 = []
var 士兵
for(var i=0; i<100; i++){
士兵 = {
ID: i, // ID 不能重复
兵种:"美国大兵",
攻击力:5,
生命值:42,
行走:function(){ /*走俩步的代码*/},
奔跑:function(){ /*狂奔的代码*/ },
死亡:function(){ /*Go die*/ },
攻击:function(){ /*糊他熊脸*/ },
防御:function(){ /*护脸*/ }
}
士兵们.push(士兵)
}
兵营.batchMake(士兵们)
// 100
var soldiers = []
var soldier
var soldierCommon = {
兵种:"美国大兵",
攻击力:5,
行走:function(){ /*走俩步的代码*/},
奔跑:function(){ /*狂奔的代码*/ },
死亡:function(){ /*Go die*/ },
攻击:function(){ /*糊他熊脸*/ },
防御:function(){ /*护脸*/ }
}
for(var i=0; i<100; i++){
soldier = {
ID: i, // ID 不能重复
生命值:42,
}
soldier.__proto__ = soldierCommon
soldiers.push(soldier)
}
兵营.batchMake(soldiers)
// 构造函数
function createSoldier(){
var obj = {
ID: i, // ID 不能重复
生命值:42,
}
obj.__proto__ = createSoldier.prototype
return obj
}
createSoldier.prototype = {
兵种:"美国大兵",
攻击力:5,
行走:function(){ /*走俩步的代码*/},
奔跑:function(){ /*狂奔的代码*/ },
死亡:function(){ /*Go die*/ },
攻击:function(){ /*糊他熊脸*/ },
防御:function(){ /*护脸*/ }
}
var soldiers = []
for(var i=0; i<100; i++){
soldiers.push(createSoldier())
}
兵营.batchMake(soldiers)
// JS 之父的关怀
function createSoldier(name){
// this = {}
// this.__proto__ = createSoldier.prototype
this.ID = i // ID 不能重复
this.生命值 = 42
this.name = name || '无名战士'
// return this
}
// createSoldier.prototype = {constructor: createSoldier}
createSoldier.prototype.兵种 = "美国大兵"
createSoldier.prototype.攻击力 = 5
createSoldier.prototype.行走 = function(){ /*走俩步的代码*/},
createSoldier.prototype.奔跑 = function(){ /*狂奔的代码*/ },
createSoldier.prototype.死亡 = function(){ /*Go die*/ },
createSoldier.prototype.攻击 = function(){ /*糊他熊脸*/ },
createSoldier.prototype.防御 = function(){ /*护脸*/ }
var soldiers = []
for(var i=0; i<100; i++){
soldiers.push(new createSoldier())
}
兵营.batchMake(soldiers)
// 习俗
1. 构造函数首字母大写
2. 构造函数可以省掉 create
3. 如果构造函数没有参数,那么可以省略括号
function Soldier(name){
this.ID = i // ID 不能重复
this.生命值 = 42
this.name = name || '无名战士'
}
// createSoldier.prototype = {constructor: createSoldier}
Soldier.prototype.兵种 = "美国大兵"
Soldier.prototype.攻击力 = 5
Soldier.prototype.行走 = function(){ /*走俩步的代码*/},
Soldier.prototype.奔跑 = function(){ /*狂奔的代码*/ },
Soldier.prototype.死亡 = function(){ /*Go die*/ },
Soldier.prototype.攻击 = function(){ /*糊他熊脸*/ },
Soldier.prototype.防御 = function(){ /*护脸*/ }
var soldiers = []
for(var i=0; i<100; i++){
soldiers.push( new Soldier )
}
兵营.batchMake(soldiers)