美文网首页
JS 中的 new 是什么

JS 中的 new 是什么

作者: 饥人谷_刘氏熏肉大饼 | 来源:发表于2018-03-24 18:54 被阅读0次

参见: 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)

相关文章

  • JS 中的 new 是什么

    参见: https://zhuanlan.zhihu.com/p/23987456注: 文章中提到士兵.原型 = ...

  • js中的new

    output:

  • js中的new()

    new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。 语法 new constructo...

  • JS中的new

    现在写一个函数,这个函数的返回值是一个对象,来观察一下这个对象,和函数体内this指向 ①,对象a就是一个空对象 ...

  • new()做了什么

    js new到底干了什么,new的意义是什么? 一、学过JS的都知道 创建对象可以这样 用内置的函数对象来构造对象...

  • js new 运行机制

    js手札--js中new到底做了些啥JS核心系列:理解 new 的运行机制深入理解 Javascript 运行机制及原型

  • JS中new详解

    new对象底层发生了什么 new 一个实例对象的底层实际就3步 1.创建一个 Object 对象 2.让新创建的对...

  • 实现js中的new

    观察 观察new的结果,rec1是一个对象,对象被赋予了构造函数的属性,并根据传参赋予属性相应的值。并且rec1的...

  • js面试题--new的原理

    JS中的new操作符 和其他高级语言一样,JS中也有new运算符,我们知道new运算符是用来实例化一个类,从而在内...

  • JS 中 this 指向及继承

    在理解继承之前,需要知道 js 的三个东西: 什么是 JS 原型链 this 的值到底是什么 JS 的new 到底...

网友评论

      本文标题:JS 中的 new 是什么

      本文链接:https://www.haomeiwen.com/subject/lefccftx.html