具有命名参数的构造函数?

时间:2016-04-14 01:13:14

标签: javascript ecmascript-6

所以当我发现我可以像这样命名参数时,我环顾四周:

function boo({first = "one", second = "two"} = {}) {
  console.log(first + second);
}

// and then calling it

boo({first = "two", second = "five"}); // logs "twofive"
boo({first = "two"}); // logs "twotwo"
boo({second = "five"}); // logs "onefive"
boo(); // logs "onetwo"

但是构造函数呢,比如这个呢?

function foo({x, y = "y", z = "z"} = {}) {
    this.x = x;
    this.y = y;
    this.z = z;
}

var bar = new foo("x");

console.log(bar);

// up until now, it works!

var rows = ["a","b","c","d","e","f","g","h","i"];
var cols = ["a","b","c","d","e","f","g","h","i"];

var foos = {};

for(let i = 0; i < rows.length; i++) { // make rows
    for(let j = 0; j < cols.length; j++) {
        let counter = rows[i] + cols[j];
        foos[counter] = new foo({x: counter});
    }
}

// this doesn't work for some reason?

实际上,代码的第二部分在chrome 49中给出了以下错误:Uncaught TypeError: foo is not a constructor

我的意思是,显然foo是一个构造函数,为什么我只能创建具有不同名称foos的81个属性,所有属性都是包含xy的对象,和z

  

修改

上面的代码似乎工作正常,但是当我尝试将它应用于更大的代码时,如下所示,它只是不想听:

&#13;
&#13;
$(function() {

  function cell({
    coords, building = "none", terrain = "soft", temperature = "25°C", humidity = "none", population = "0", money = "$0", income = "$0", production_amount = "0", production_type = "none", corruption_level = "0%", owner = "none"
  } = {}) {
    this.coords = coords;
    this.building = building;
    this.terrain = terrain;
    this.temperature = temperature;
    this.humidity = humidity;
    this.population = population;
    this.money = money;
    this.income = income;
    this.production_amount = production_amount;
    this.production_type = production_type;
    this.corruption_level = corruption_level;
    this.owner = owner;
  }

  // var cella = new cell("aa");
  //
  // console.log(cella);

  var rows = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
  var cols = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

  var cells = {};

  for (let i = 0; i < rows.length; i++) { // make rows
    for (let j = 0; j < cols.length; j++) {
      let coords = rows[i] + cols[j];
      let cell = "<div class=\"cell\" id=\"" + coords + "\"></div>";
      $("main").append(cell);
      cells[coords] = new cell({
        coords: coords
      });
    }
  }

  $("div.cell").click(function() {
    console.log(this.id);
  });

});
&#13;
body {
  margin: 0;
}
main {
  width: 100vw;
  height: 100vh;
}
main div.cell {
  width: calc(100vw / 9);
  height: calc(100vh / 9);
  background-color: #9E1023;
  /*border: solid 1px black;*/
  float: left;
}
main div.cell:hover {
  background-color: #740B20;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Evalitia</title>
</head>

<body>
  <main></main>
</body>

</html>
&#13;
&#13;
&#13;

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您必须以调用常规函数的方式调用构造函数,并将对象作为参数。

var bar = new foo({ x: "x" });

所以for循环应该是:

&#13;
&#13;
function foo({ x, y = "y", z = "z" } = {}) {
  this.x = x;
  this.y = y;
  this.z = z;
}
var rows = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
var cols = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

var foos = {};

for (let i = 0; i < rows.length; i++) { // make rows
  for (let j = 0; j < cols.length; j++) {
    let counter = rows[i] + cols[j];
    foos[counter] = new foo({
      x: counter
    });
  }
}

document.getElementById('result').innerHTML = JSON.stringify(foos, null, 1);
&#13;
<div id="result"></div>
&#13;
&#13;
&#13;