如何在运行时在javascript中创建对象的多个实例?

时间:2015-06-21 22:16:03

标签: javascript object

尝试完成作业,我似乎找不到这个: 这是问的问题:

"您的JavaScript程序应解析用户的输入。对于每个输入,您应该创建Shopper的JavaScript对象以保存从用户输入解析的数据。确保每个对象都应该有一个名为“Items”的属性,该属性应该有一个字符串数组作为值,例如[“Milk”,“Shoes”,“Phone”]。"

这是我到目前为止所做的:

var shopper = {
  first_name : "",
  last_name : "",
  email  : "",
  items: []
};

var flag = true;
var x = 0;
while (flag == true) {

  var temp_obj = prompt("Please enter your first name, last name, email and items separated by ,");
  if (temp_obj == "" || temp_obj == null) {
    flag = false;
  }

/*Here I'm suppose to create instances of the object..Have no idea how. this is what I think might work.
var shopper[x] = Object.create(shopper);
x++
*/

}

3 个答案:

答案 0 :(得分:2)

您可以使用new运算符来实例化构造函数:

function Shopper(str) {
  this.items = str.split(', ');
  this.first_name = this.items[0];
  this.last_name = this.items[1];
  this.email = this.items[2];
}
var str;
while(str = prompt("Please enter...")) {
  var instance = new Shopper(str);
}

答案 1 :(得分:2)

这是使用工厂功能

的一种方法

单击运行代码段以查看其是否有效。

要完成输入项目,请单击空白行确定



// Shopper constructor
function Shopper(firstName, lastName, email, items) {
  this.firstName = firstName;
  this.lastName  = lastName;
  this.email     = email;
  this.items     = items;
}

// Shopper factory function
// takes user input with comma-separated terms
// inputs[0] = firstName
// inputs[1] = lastName
// inputs[2] = email
// inputs[3..] = items
Shopper.create = function create(str) {
  var inputs = str.split(', ');
  return new Shopper(inputs[0], inputs[1], inputs[2], inputs.slice(3));
};

// a list of all shoppers
var shoppers = [];

// continue collecting user inputs until user enters a blank line
var response;
while (response = prompt("firstName, lastName, email, item1, item2, item3, ...")) {

  // add shopper to the `shoppers` array
  shoppers.push(Shopper.create(response));
}

// display all shoppers
alert(JSON.stringify(shoppers, null, "\t"));




示例用户输入

a, b, c@example.com, milk, candy, eggs
x, y, z@example.com, meat, cereal, yams

输出

[
    {
        "firstName": "a",
        "lastName": "b",
        "email": "c@example.com",
        "items": [
            "milk",
            "candy",
            "eggs"
        ]
    },
    {
        "firstName": "x",
        "lastName": "y",
        "email": "z@example.com",
        "items": [
            "meat",
            "cereal",
            "yams"
        ]
    }
]

答案 2 :(得分:0)

作业中没有任何内容表明您必须使用prompt。它只是说。

  

您的JavaScript程序应解析用户的输入。

这是一个使用HTML表单创建对象的javascript程序。

var form = document.forms[0];
var btn = document.getElementById("create");

btn.onclick = function(event){
  var shopper = {
    first_name: form.first_name,
    last_name: form.last_name
  };
  
  console.log(obj);
  
  event.preventDefault();
};
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.css" rel="stylesheet"/>
<form id="shopper_form">
  
  <div class="row">
    <label>First name</label>
    <input type="text" name="first_name"/>
  </div>
  
  <div class="row">
    <label>Last name</label>
    <input type="text" name="last_name"/>
  </div>
  
  <a class="button" href="#" id="create">Create</a>
</form>

<div id="test"></div>