我在这样的app.js文件中有一个数组:
var data = [
{id:1, firstName: 'John', lastName: 'Smith'},
{id:2, firstName: 'Jane', lastName: 'Smith'},
{id:3, firstName: 'John', lastName: 'Doe'}
];
现在,我正在使用Node.js并尝试在module.js文件中创建函数调用addEmployee()。 addEmplyee()只需要两个argurments,即添加的firstName和lastName。应使用underscore.js将id值计算为比当前最大id多一个。 这是我在module.js文件中的代码,但它不起作用。
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
}
var person = new Person(id, firstName, lastName);
data.push(person);
return data;
}
}
}
有人可以帮忙吗?非常感谢你!
答案 0 :(得分:0)
试试这个:
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = (function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
})(); //IIFE to invoke function immidiately and thus assign value to id
data.push({id: this.id,firstName: this.firstName,lastName: this.lastName});
return data;
}
}
}
答案 1 :(得分:0)
应该是这样的:
var _ = require('underscore');
// import data
module.exports = {
addEmployee: function (firstName, lastName) {
// remove id
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = (function (data) {
var id = _.max(data, function (data) {
return data.id;
});
return id.id + 1;
})();
}
// put OUTSIDE - dont need id here
var person = new Person(firstName, lastName);
data.push(person);
return data;
}
}
答案 2 :(得分:0)
首先,你无论如何都不会调用这个内部函数/ contstructor,因此它不会被添加到数组中。其次,不要在函数内定义Person
(它将在每次调用时定义为新事物 - 每个新Person
都是新类型)。第三件事是,如果你调用你的代码,它会导致无限循环(在构造函数中调用new on self会在下一个新实例中执行相同操作,依此类推)。另外,您在哪里定义/导入数据?
var _ = require('underscore');
// probably it'd be even better to move this to separate module
function Person(id, firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
};
module.exports = {
addEmployee: function (firstName, lastName) {
data.push(new Person((_.max(data, function (data) {
return data.id;
}) + 1), firstName, lastName));
}
}
我还建议使用lodash
代替undescore
(直接替换,但写得更好)。
如果您只需要保留数据{ id, firstName, lastName }
的对象,则不需要新类型Person
:
module.exports = {
addEmployee: function (firstName, lastName) {
var id = (_.max(data, function (data) {
return data.id;
}) + 1);
data.push({ id: id, firstName: firstName, lastName: lastName });
}
}
如果使用节点6.X或4.X +(不确定es6表示法是否适用于所有4.X,肯定4.4+将使用'use strict';
,它可能更短:
'use strict';
var _ = require('underscore');
module.exports = {
addEmployee: function (firstName, lastName) {
var id = (_.max(data, function (data) {
return data.id;
}) + 1);
data.push({ id, firstName, lastName });
}
}
最后,我建议不要改变数据(副作用主要是坏的) - 我将其转换为类似的东西(假设可以执行es6的节点):
'use strict';
const _ = require('underscore');
module.exports = {
// note that data comes as input
addEmployee: function (data, firstName, lastName) {
// obtain next id
const id = (_.max(data, function (data) {
return data.id;
}) + 1);
// return new data (old data + new object, but without mutation of initial object)
return [...data, { id, firstName, lastName }];
}
}