定义对象JavaScript的功能

时间:2018-02-27 18:41:56

标签: javascript function object

如何定义对象中的函数?

var allData = [
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
{type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
{type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "dbennett@gmail.com", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "jstevens22@hotmail.com", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "spym99@hotmail.com", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "steven2231@hotmail.com", address_id: 1836, add_date: null}},    
];

var CustomerDB = {customers:[], addresses:[], stores:[], insertData, addCustomer}

CustomerDB.insertData = function (objData){
    objData.forEach(function(element){
    if (element.type == "customer"){
        CustomerDB.addCustomer(element.data);
    };
    });
};


CustomerDB.addcustomer = function (customerObj){
    customerObj.add_date= new Date();
    CustomerDB.customers.push(customerObj);
};

CustomerDB.insertData(allData);
console.log(CustomerDB.customers);

我一直收到没有定义insertData的错误。我需要做什么?

2 个答案:

答案 0 :(得分:0)

insertDataaddCustomer是CustomerDB对象的属性,而不是像您尝试过的那样可以分配给CustomerDB的function expressions。如果从CustomerDB insertDataaddCustomer删除这两个技术上未定义的变量(解释器只知道addCustomer和insertData作为CustomerDB的属性),那么它将正常工作:

var CustomerDB = {customers:[], addresses:[], stores:[]}

CustomerDB.insertData = function (objData){
    objData.forEach(function(element){
    if (element.type == "customer"){
        CustomerDB.addCustomer(element.data);
    };
    });
};


CustomerDB.addCustomer = function (customerObj){
    customerObj.add_date= new Date();
    CustomerDB.customers.push(customerObj);
};

CustomerDB.insertData(allData);
console.log(CustomerDB.customers);

使用分配给变量的函数的替代方法:

var insertData = function(objData) {
    objData.forEach(function(element){
      if (element.type == "customer"){
        CustomerDB.addCustomer(element.data);
      };
    });
};


var addCustomer = function(customerObj) {
    customerObj.add_date= new Date();
    CustomerDB.customers.push(customerObj);
};

var CustomerDB = {customers:[], addresses:[], stores:[], insertData, addCustomer}

CustomerDB.insertData(allData);
console.log(CustomerDB.customers);

最后一点:这是ES5语法,我强烈建议您探索ES6

希望有所帮助

答案 1 :(得分:-1)

``

重写您的代码,如下所示

var allData = [
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
{type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
{type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "dbennett@gmail.com", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "jstevens22@hotmail.com", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "spym99@hotmail.com", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "steven2231@hotmail.com", address_id: 1836, add_date: null}},    
];

var CustomerDB = {customers:[], addresses:[], stores:[]}

CustomerDB.insertData = function (objData){
    objData.forEach(function(element){
    if (element.type == "customer"){
    CustomerDB.customers.push(element.data);
    };
    });
};
CustomerDB.insertData(allData);
console.log(CustomerDB.customers);