不确定参数是如何传递给参数的
Unsure how argument is being passed to parameter
我正在编写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];
}
}
};
只是手拉还是什么的?我把它放在一个文件中,它没有记录任何内容。CCD_ 1和CCD_。这就是它在他们的结果沙盒中显示的内容:
{ 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' ] }
这似乎是错误的,因为史蒂夫被重复了。或者Steve被登录/返回,但Bill没有。但就像我说的那样,它过去了。我很困惑。
我的问题是为什么friends[j].firstName === name
对搜索功能有任何意义?更具体的名称。简化/不迭代它,它说的是
if (friends.bill.firstName === name) {
console.log(friends.bill);
return friends.bill;
}
但name
在哪里定义?
变量name
在函数声明中定义为参数:function(name) {
该函数将循环遍历对象friends
并获取其所有属性,如bill
和steve
(它们也是对象(。然后,它将查看这些对象的search();
0属性是否与您发送给函数的参数相同。
因此,要查看登录到控制台的内容,您必须调用您创建的函数,并将一些现有名称作为参数传递,例如:
search('Steve');
然后,它将记录整个steve
对象:
Object { firstName: "Steve", lastName: "Johnson", number: "666-666-6666", address: Array[4] }
您有一个包含两个人的对象和一个带参数name
的搜索函数function search(name){...}
,
尝试
search("Steve");
search(friends.steve.firstName);
search(friends.bill.firstName);
并检查控制台。
您应该在末尾的某个位置运行函数:search("Bill");
…你只记录一些东西,如果搜索找到了什么。。。
编辑name
是函数的参数,使用search("Bill")
可以将字符串"Bill"
传递给函数,name
在本例中是"Bill"
。
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.
*/
- 在Flickr API的标签参数中传递一个变量
- 如何知道函数的参数中传递的var名称
- javascript将函数中一个变量的参数值传递给父函数中的另一个
- Jquery-将参数动态传递给函数
- 通过参数将传递的函数分配给动态创建的输入类型的onclick事件
- 使用jQuery deffered.resolveWith()在单个参数中传递数组
- Javascript 函数在与参数一起传递时无法调用
- 将参数值传递给按钮
- 将数组作为参数列表传递给具有现有参数的函数
- 单击窗口.位置,参数已传递
- 在一个参数中传递两个值的 URL 字符串
- 为什么这些参数没有传递给承诺
- 子类化 JavaScript 构造函数参数不传递给数组
- 在流星订阅参数中传递给服务器的内容
- 在url查询参数中传递多个url
- RequireJS:配置参数未传递到包中
- 如何在@Html.ActionLink中的Querystring参数中传递String
- 在一个参数中传递多个参数的对象
- 如何将按钮对象作为函数参数进行传递
- JavaScript在参数中传递新数组