2015-09-20 73 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); 

並檢查控制檯。