2015-09-20 74 views
0

我在做codecademy javascript。没有编程经验。这让我通过这轮,但我不明白如何从if语句到这个函数争论名字。不确定如何将参数传递给参数

var friends = { 
    bill: { 
     firstName: "Bill", 
     lastName: "Davidson", 
     number: "555-555-5555", 
     address: ['1', '2', '3', '4'] 
    }, 
    steve: { 
     firstName: "Steve", 
     lastName: "Johnson", 
     number: "666-666-6666", 
     address: ['5', '6', '7', '8'] 
    } 
}; 

var search = function(name) { 
    for(var j in friends) { 
     if(friends[j].firstName === name) { 
      console.log(friends[j]); 
      return friends[j]; 
     } 
    } 
}; 

它只是手持或什么?我把它放在一个文件中,它没有记录任何东西。 search();search(friends.bill);也没有记录任何东西。这是它在结果沙盒中显示的内容:

{ firstName: 'Steve', 
    lastName: 'Johnson', 
    number: '666-666-6666', 
    address: [ '5', '6', '7', '8' ] } 
{ firstName: 'Steve', 
    lastName: 'Johnson', 
    number: '666-666-6666', 
    address: [ '5', '6', '7', '8' ] } 
{ firstName: 'Bill', 
    lastName: 'Davidson', 
    number: '555-555-5555', 
    address: [ '1', '2', '3', '4' ] } 

这似乎是错误的,因为史蒂夫重复。或史蒂夫登录/返回,但比尔不是。但就像我说的那样。我很困惑。


我的问题是为什么friends[j].firstName === name对搜索功能有什么意义?更具体的名称。简化/不迭代它,它说

if (friends.bill.firstName === name) { 
    console.log(friends.bill); 
    return friends.bill; 
} 

但如果定义name

+1

真的不清楚你在这里问什么。 – Pointy

+0

添加:'search(friends.bill)'传递'bill'对象。如果你想查看结果,你可能会想要搜索(friends.bill.firstName)或搜索('Bill')。 – Anchor

+0

'var search = function(name){'...这是定义名称的位置,它是搜索函数的函数“参数”。也许你对'var search = function(name){'语法 - 它相当于'function search(name){' –

回答

2

可变name在函数的声明定义的,如一个参数:function(name) {

函数将遍历对象friends并获得其所有属性,像billsteve(它们也是对象)。然后,它会查看这些对象的firstName属性是否与您发送给该函数的参数相同。然后

search('Steve'); 

,它会记录下整个steve对象:

所以,看到的东西登录到控制台,你必须叫你所创建的功能,并通过一些现有的名称作为参数,如:

Object { firstName: "Steve", lastName: "Johnson", number: "666-666-6666", address: Array[4] } 
+0

总体意义。我过于复杂了。谢谢 – delz

0

你应该结束的地方运行功能: search("Bill"); ......你只记录的东西,如果搜索找到的东西......

编辑 name是你的函数的参数,用search("Bill")你传递字符串"Bill"到函数name在这种情况下是"Bill"

-1

var friends = { 
 
    bill: { 
 
     firstName: "Bill", 
 
     lastName: "Davidson", 
 
     number: "555-555-5555", 
 
     address: ['1', '2', '3', '4'] 
 
    }, 
 
    steve: { 
 
     firstName: "Steve", 
 
     lastName: "Johnson", 
 
     number: "666-666-6666", 
 
     address: ['5', '6', '7', '8'] 
 
    } 
 
}; 
 

 
/* 
 
So at this point you've just created an object called "friends" 
 
and within it are two friends whose names are either "steve" or "bill" 
 
*/ 
 

 
//------------------------------------------ 
 

 
/* 
 
Then you create a function below and pass in the "parameter" name, which can really be anything, but I'm assuming it's being passed from some event. 
 
*/ 
 

 
var search = function(name) { 
 
    for(var j in friends) { 
 
     /* 
 
     Here, your for loop becomes a loop that loops for every instance in 
 
     friends. There are 2 friends, so your loop will execute twice for the 
 
     two objects within your "friends" object, they are object 0, and object 1 
 
     because arrays are a zero index object. 
 
     */ 
 
     if(friends[j].firstName === name) { 
 
      /* 
 
      Here, you are saying to your function, that IF the friend object at 
 
      the array index value of [j] (which will be 0, and then 1) matches 
 
      the parameter(argument) passed in as "name" then log it, and 
 
      subsequently return the object values. If not, do nothing. 
 
      */ 
 
      console.log(friends[j]); 
 
      return friends[j]; 
 
      /*For example, if you passed your function(name){//do stuff here;} 
 
      as function(bill){ 
 
       /* okay user, I've got this if statement which makes me loop 
 
       the friends object and validate if I see something matching. 
 
       .......is object 1 within friends "bill" ? YES! 
 
        ....then I'll pass obj.Bill back to the user (return) and 
 
        ..I'll show the user this as "console.log(friends[0])" 
 
       .......is the next object "bill" ? No. 
 
       .......are there anymore objects? No. K, we're done. 
 
       */ 
 
     } 
 
    } 
 
}; 
 

 
/* NOTE: Don't get confused with "j" as the looping variable as it's just going to continue for the length of the object it uses to validate your argument against. If you had 50 names in obj.Friends then it'd loop 50 times as a zero index array (index of 0-49) 
 
*/

我评论过上面的代码片段,所以你可以看到更多细节发生的每个阶段。我希望它有帮助,当我第一次开始时,我遇到了数组和对象的问题......完全是在同一个网站上!;)

这里是简化的条件相同的代码:

var function = someFunction(parameter){ 
for(var counter = 0; counter < friends.length; friends +=1){ 
if((friends[counter].bill) || /*or*/ (friends[counter].steve)){ 
return friends[counter]; 
console.log friends[counter]; 
} 
/* 
1 - "var functionName = function(parameter){ //do stuff };" is used 
because it stores the function in memory and allows you to call it 
from inside your web view, say on a button press you can use the button 
press to provide "(name)" to be checked against all the objects in your 
"friends" object." 
2 - Using "name" as a generic parameter, and checking it against 
all of the objects by name within "friends" just allows you to make 
a virtually limitless searching feature. It's good coding, although 
at first it's hard to grasp why they do this. But image you have a 
search feature for an application like "Facebook" with 1 billion users. 
3 - The function is stored in memory this way and will logically 
continue looping through the entire friends object until the 
parameter given to the function, matches or not. If it matches 
it'll "return" it, and "log" it. 
4 - Testing is paramount to becoming a good coder. Typically, you'll 
wireframe your functions like this and remove the console.log comments 
upon release. 
*/ 
1

你已经在这两个人,用一个参数name搜索功能function search(name){...}一个对象,

尝试

search("Steve"); 
search(friends.steve.firstName); 
search(friends.bill.firstName); 

并检查控制台。