有人可以解释一下这个功能的每一行是做什么的吗? [英] Could someone please explain what each line of this function is doing?

查看:53
本文介绍了有人可以解释一下这个功能的每一行是做什么的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题:

每个:

<前lang =Javascript> 功能(列表,迭代器){
if (list == null 返回 true ;
iterator = iterator || .identity;
var keys = list.length!== + list.length&& this .keys(list),
length =(keys || list)。length,
index,currentKey;
for (index = 0 ; index< length; index ++){
currentKey =键? keys [index]:index;
if (!iterator(list [currentKey],currentKey,list)) return ;
}
返回 true ;

}







第二个问题(不需要逐行指导):



每个:

  function (list,iterator){
var i,length;
if (isArrayLike(list)){
for (i = 0 ,length = list.length; i< length; i ++){
iterator(list [i],i,list);
}
}
其他 {
var 键= Object.keys(list);
for (i = 0 ,length = keys.length; i< length; i ++ ){
iterator(list [keys [i]],keys [i],list);
}
}
}





为什么你需要在这个函数中完成迭代器所有三个(列表[i],我和列表)?



谢谢!

解决方案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一段解释!例如:

 int next = r.Next(); 



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 您是否想要坐下45分钟并且没有充分理由键入逐行描述?


转到http://www.w3schools.com/js/default.asp [ ^ ]你将能够了解它的全部内容。


你要求的是一个巨大的因为你对这段代码的了解很少而工作。你问这个事实会让你需要很多解释。



第一个解决方案:

- 聘请顾问或者问一个朋友/老师比你更熟练。



第二个解决方案:

- 使用调试器看代码执行,你会看到它是什么确实。你理解它可能已经足够了。通过查看它的作用,你可以理解为什么。


First Question:
every:

function (list, iterator) {
    if (list == null) return true;
    	iterator = iterator || this.identity;
    	var keys = list.length !== +list.length && this.keys(list),
    		length = (keys || list).length,
    		index, currentKey;
    	for (index = 0; index < length; index++) {
    		currentKey = keys ? keys[index] : index;
    		if (!iterator(list[currentKey], currentKey, list)) return false;
    	}
    	return true;
    
    }




Second Question (no need for line by line instruction):

each:

function (list, iterator) {
    var i, length;
    if (isArrayLike(list)) {
    	for (i = 0, length = list.length; i < length; i++) {
    		iterator(list[i], i, list);
    	}
    }
    else {
    	var keys = Object.keys(list);
    	for (i = 0, length = keys.length; i < length; i++) {
    		iterator(list[keys[i]], keys[i], list);
    	}
    }
}



How come in this function you have to do the iterator over all three of the (list[i], i, and list)?

Thanks!

解决方案

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:

int next = r.Next();


Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?


Go to http://www.w3schools.com/js/default.asp[^] and you will be able to learn all about it.


What you request is a huge work because of your little understanding of this code. The fact that you ask this makes that you will need a lot of explanations.

First solution:
- Hire a consultant or ask a friend/teacher more skilled than you.

Second Solution:
- Use the debugger to see the code execute, you will see what it does. It may be enough for you to understand it. By seeing what it does, you may understand why.


这篇关于有人可以解释一下这个功能的每一行是做什么的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆