如何一次调用同一个构造函数中的所有对象?
How do I call all objects from the same constructor function at once?
我是 javascript 的新手。我想一次过滤所有 Employee 对象和 return 工资等于或超过 300 的人的名字。然后我想将这些结果存储到一个数组中。我不知道如何在所有对象上调用方法 filterSalaries()
。请帮助我。
var EmployeeBlueprint = {
all_info: function () {
console.log("Employee: " + this.name + " has a salary of $" + this.salary + ".");
},
isMale: function() { return this.gender == "Male"; },
isFemale: function() { return this.gender == "Female"; },
filterSalaries: function() {
if (this.salary >= 300) {
return this.name;
}
}
};
function Employee (name, salary, gender) {
this.name = name;
this.salary = salary;
this.gender = gender;
};
Employee.prototype = EmployeeBlueprint;
var Matt = new Employee("Matt", 100, "Male");
var Alex = new Employee("Alex", 200, "Male");
var Zack = new Employee("Zack", 300, "Male");
var Mark = new Employee("Mark", 400, "Male");
var Rick = new Employee("Rick", 500, "Male");
摆脱那些变量并将它们存储在一个数组中开始。
var emps = [
new Employee("Matt", 100, "Male"),
new Employee("Alex", 200, "Male"),
new Employee("Zack", 300, "Male"),
new Employee("Mark", 400, "Male"),
new Employee("Rick", 500, "Male")
];
然后 .filter()
数组以获取您的缩减集,.map()
设置为名称。
var low_salary_names = emps.filter(function(emp) {
return emp.salary <= 300;
}).map(function(emp) {
return emp.name;
});
我是 javascript 的新手。我想一次过滤所有 Employee 对象和 return 工资等于或超过 300 的人的名字。然后我想将这些结果存储到一个数组中。我不知道如何在所有对象上调用方法 filterSalaries()
。请帮助我。
var EmployeeBlueprint = {
all_info: function () {
console.log("Employee: " + this.name + " has a salary of $" + this.salary + ".");
},
isMale: function() { return this.gender == "Male"; },
isFemale: function() { return this.gender == "Female"; },
filterSalaries: function() {
if (this.salary >= 300) {
return this.name;
}
}
};
function Employee (name, salary, gender) {
this.name = name;
this.salary = salary;
this.gender = gender;
};
Employee.prototype = EmployeeBlueprint;
var Matt = new Employee("Matt", 100, "Male");
var Alex = new Employee("Alex", 200, "Male");
var Zack = new Employee("Zack", 300, "Male");
var Mark = new Employee("Mark", 400, "Male");
var Rick = new Employee("Rick", 500, "Male");
摆脱那些变量并将它们存储在一个数组中开始。
var emps = [
new Employee("Matt", 100, "Male"),
new Employee("Alex", 200, "Male"),
new Employee("Zack", 300, "Male"),
new Employee("Mark", 400, "Male"),
new Employee("Rick", 500, "Male")
];
然后 .filter()
数组以获取您的缩减集,.map()
设置为名称。
var low_salary_names = emps.filter(function(emp) {
return emp.salary <= 300;
}).map(function(emp) {
return emp.name;
});