JS:为类似函数获取不同的值

时间:2016-08-03 03:11:05

标签: javascript

下面我有两个代码片段,两个代码片段具有相同的对象和功能。所以他们都应该返回相同的值。 我能看到的唯一区别是底部代码片段中函数的'for循环'中添加了括号和'else'语句。

为什么每个代码段中的函数返回不同的值而不是相同的值?感谢

var employees = [{
  firstName: 'Ollie',
  lastName: 'Hepburn',
  role: 'Boss'
}, {
  firstName: 'Morty',
  lastName: 'Smith',
  role: 'Truck Driver'
}, {
  firstName: 'Peter',
  lastName: 'Ross',
  role: 'Warehouse Manager'
}, {
  firstName: 'Cal',
  lastName: 'Neil',
  role: 'Sales Assistant'
}, {
  firstName: 'Jesse',
  lastName: 'Saunders',
  role: 'Admin'
}, {
  firstName: 'Anna',
  lastName: 'Jones',
  role: 'Sales Assistant'
}, {
  firstName: 'Carmel',
  lastName: 'Hamm',
  role: 'Admin'
}, {
  firstName: 'Tori',
  lastName: 'Sparks',
  role: 'Sales Manager'
}, {
  firstName: 'Peter',
  lastName: 'Jones',
  role: 'Warehouse Picker'
}, {
  firstName: 'Mort',
  lastName: 'Smith',
  role: 'Warehouse Picker'
}, {
  firstName: 'Anna',
  lastName: 'Bell',
  role: 'Admin'
}, {
  firstName: 'Jewel',
  lastName: 'Bell',
  role: 'Receptionist'
}, {
  firstName: 'Colin',
  lastName: 'Brown',
  role: 'Trainee'
}];

function findEmployeesRole(name) {
  for(var i = 0; i < employees.length; i++)
    if(name === employees[i].firstName + " " + employees[i].lastName) return employees[i].role;
  return "Does not work here!";
}
                                      
                                      console.log(findEmployeesRole("Colin Brown"));

以下代码段中对象下面的函数在for循环中有括号。

var employees = [{
  firstName: 'Ollie',
  lastName: 'Hepburn',
  role: 'Boss'
}, {
  firstName: 'Morty',
  lastName: 'Smith',
  role: 'Truck Driver'
}, {
  firstName: 'Peter',
  lastName: 'Ross',
  role: 'Warehouse Manager'
}, {
  firstName: 'Cal',
  lastName: 'Neil',
  role: 'Sales Assistant'
}, {
  firstName: 'Jesse',
  lastName: 'Saunders',
  role: 'Admin'
}, {
  firstName: 'Anna',
  lastName: 'Jones',
  role: 'Sales Assistant'
}, {
  firstName: 'Carmel',
  lastName: 'Hamm',
  role: 'Admin'
}, {
  firstName: 'Tori',
  lastName: 'Sparks',
  role: 'Sales Manager'
}, {
  firstName: 'Peter',
  lastName: 'Jones',
  role: 'Warehouse Picker'
}, {
  firstName: 'Mort',
  lastName: 'Smith',
  role: 'Warehouse Picker'
}, {
  firstName: 'Anna',
  lastName: 'Bell',
  role: 'Admin'
}, {
  firstName: 'Jewel',
  lastName: 'Bell',
  role: 'Receptionist'
}, {
  firstName: 'Colin',
  lastName: 'Brown',
  role: 'Trainee'
}];

function findEmployeesRole(name) {
  for (var i = 0; i < employees.length; i++){
    if(name === employees[i].firstName + " " + employees[i].lastName){
      return employees[i].role;
    } else {
      return "Does not work here!";
    }
  }
}

console.log(findEmployeesRole("Colin Brown"));

1 个答案:

答案 0 :(得分:0)

在第二组代码中,它只能检查第一个项目。或者,你在第一个项目上匹配并返回角色,否则你返回&#34;在这里不起作用&#34;,循环没有机会继续。

在第一段代码中,return语句实际上在循环之外,这意味着它只返回&#34;这里不起作用&#34;直到所有项目都被检查。使用大括号,它看起来像这样:

function findEmployeesRole(name) {
    for (var i = 0; i < employees.length; i++) {
        if (name === employees[i].firstName + " " + employees[i].lastName) {
            return employees[i].role;
        }
    }
    return "Does not work here!";
}

通常认为总是使用牙套来防止这样的情况是好的做法,因为乍看之下并没有完全清楚发生了什么。