JS 中的 new 是什么

參見: https://zhuanlan.zhihu.com/p/23987456
注: 文章中提到士兵.原型 = {} 的原因是因為如果單獨寫: 原型={}, 這個對象看起來會和 士兵={}沒有明顯的聯(lián)系, 所以將共有屬性以士兵.原型 = {} 的方式存儲, 看起來關聯(lián)緊密

var 士兵 = {
  ID: 1, // 用于區(qū)分每個士兵
  兵種:"美國大兵",
  攻擊力: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)

// 構(gòu)造函數(shù) 
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 || '無名戰(zhàn)士'
    // 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. 構(gòu)造函數(shù)首字母大寫
2. 構(gòu)造函數(shù)可以省掉 create
3. 如果構(gòu)造函數(shù)沒有參數(shù),那么可以省略括號
function Soldier(name){
    this.ID = i // ID 不能重復
    this.生命值 = 42
    this.name = name || '無名戰(zhàn)士'
}
// 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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容