我正在使用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
在哪里定义?
答案 0 :(得分:2)
变量name
在函数声明中定义,作为参数:function(name) {
该函数将遍历对象friends
并获取其所有属性,如bill
和steve
(它们也是对象)。然后,它将查看这些对象的firstName
属性是否与您发送给函数的参数相同。
因此,要查看登录到控制台的内容,您必须调用已创建的函数并将一些现有名称作为参数传递,例如:
search('Steve');
然后,它将记录整个steve
对象:
Object { firstName: "Steve", lastName: "Johnson", number: "666-666-6666", address: Array[4] }
答案 1 :(得分:1)
你有一个有两个人的对象和一个带有参数function search(name){...}
的搜索功能name
,
尝试
search("Steve");
search(friends.steve.firstName);
search(friends.bill.firstName);
并检查控制台。
答案 2 :(得分:0)
你应该在最后的某个地方运行你的功能:
search("Bill");
...你只记录一些东西,如果搜索找到了什么......
修改强>
name
是您的函数的参数,search("Bill")
将字符串"Bill"
传递给函数name
,在这种情况下为"Bill"
。
答案 3 :(得分:-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)
*/

我对上面的代码片段进行了大量评论,因此您可以更详细地了解正在发生的事情的每个阶段。我希望它有所帮助,当我第一次开始时,我遇到了数组和对象的问题....在字段上在SAME网站上! ;)
以下是简化术语中的相同代码:
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.
*/