简体   繁体   English

CLI NodeJS(并发,回调,.method)

[英]CLI NodeJS (Concurrency, Callbacks, .methods)

Just a couple of questions regarding using Nodejs in Web Development. 关于在Web开发中使用Nodejs的几个问题。

1) For my Concurrency question, it regards syntax. 1)对于我的并发问题,它与语法有关。 Is there a difference between: 之间有什么区别:

setInterval(function() {
    console.log('Task A');
}, 10);

and

function setInterval() {
    console.log('Task A');
}, 10);

Also, i'm a little confused what the '10' means at the end of the method, my guess is the time it takes for the method to complete? 另外,我有点困惑方法结尾处的“ 10”是什么意思,我猜是方法完成需要花费时间吗?

2) Callbacks - Are Callbacks just technically another name in Node for testing code? 2)回调-回调在技术上是否只是Node中用于测试代码的另一个名称?

3) Is there a method I can use in the Node(CLI) to see all of the methods in a module? 3)是否可以在Node(CLI)中使用一种方法来查看模块中的所有方法?

EX: 例如:

var fs = require('fs');

Obviously there are tons of methods in the File Systems module, but like the language Ruby, using PRY in the CLI, you can type 'fs.methods', which will display all of the methods. 显然,“文件系统”模块中有很多方法,但是像Ruby语言一样,在CLI中使用PRY,您可以键入“ fs.methods”,它将显示所有方法。 Then using 'cat', you can see the contents of each individual method. 然后使用“ cat”,您可以看到每个单独方法的内容。 Something like this for Node(CLI)? 像这样的Node(CLI)?

Thanks for all of the advice/answers! 感谢您的所有建议/答案!

Cheers, G 干杯,G

1. In the first, you pass in an anonymous function which will be invoked at the interval. 1.首先,您传入一个匿名函数,该函数将在该时间间隔被调用。 Here you are using the node.js API setInterval. 在这里,您使用的是node.js API setInterval。

In the second example, you are declaring a function called setInterval. 在第二个示例中,您将声明一个名为setInterval的函数。 Looks like a syntax error is there... 似乎有语法错误...

setInterval is a function that takes 2 objects in as parameters. setInterval是一个将2个对象作为参数的函数。 That's it. 而已。 the first parameter should be a function, and the second parameter should be a the the interval time in milliseconds. 第一个参数应该是一个函数,第二个参数应该是一个间隔时间(以毫秒为单位)。 All that setInterval does is run the function passed in in the first parameter(a callback) every x milliseconds as specified in the 2nd parameter. setInterval所做的全部工作是按第2个参数中指定的每x毫秒运行在第一个参数(回调)中传入的函数。

2. No. Callbacks are functions that can be passed to other functions so that they can be "called-back" later in the code. 2.否。回调是可以传递给其他函数的函数,以便可以在代码的后面“回调”。 Callbacks are pervasive in node.js applications and tightly related to it's asynchronous event based architecture. 回调普遍存在于node.js应用程序中,并且与基于异步事件的体系结构紧密相关。 It is one of the most common patterns seen in node.js. 它是node.js中最常见的模式之一。

3. Just look in node.js api docs on their website. 3.只需在其网站上查看node.js api文档即可。

My recommendation to you would be to read about the node.js event loop and asynchronous programming. 我对您的建议是阅读有关node.js事件循环和异步编程的信息。

First off, you're asking about some pretty fundamental aspects of Javascript so I'd suggest you work though some basic Javascript training because it will be hard to learn node.js if you don't already have a core understanding of the basics of Javascript. 首先,您要询问Javascript的一些基本方面,因此我建议您通过一些基本的Java培训来工作,因为如果您尚未基本了解Java的基础知识,将很难学习node.js。 Javascript。 In particular callbacks are integral to much of nodejs coding. 特别是,回调是大部分nodejs编码所不可或缺的。

Is there a difference between these two? 两者之间有区别吗?

Yes, the two are completely different. 是的,两者是完全不同的。 One uses a built-in timer function, the other attempts to declare it's own function that has nothing to do with timers. 一种使用内置的计时器函数,另一种尝试声明与计时器无关的自己的函数。

Let me explain your two examples: 让我解释一下您的两个示例:

The built-in setInterval function 内置的setInterval函数

setInterval(function() {
    console.log('Task A');
}, 10);

Nodejs has a built-in timer function called setInterval . Nodejs具有一个内置的计时器函数setInterval You can find the doc for it here. 您可以在此处找到该文档。

You pass this function two arguments. 您向该函数传递两个参数。 The first argument is a function reference, the second argument is an amount of time in milliseconds. 第一个参数是函数引用,第二个参数是时间(以毫秒为单位)。 The nodejs timer infrastructure will call the function you passed it every N milliseconds. nodejs计时器基础结构将每N毫秒调用一次您传递给它的函数。

It might be slightly easier to understand how setInterval works by seeing it used like this: 通过查看setInterval的用法,可能会稍微容易一些:

function myFunction() {
    console.log('Task A');
}

setInterval(myFunction, 10);

This has the same output as your first example, but I think it shows more clearly how setInterval() is a built-in function that takes two arguments, a function and a number. 它具有与第一个示例相同的输出,但是我认为它更清楚地显示了setInterval()是一个内置函数,该函数带有两个参数,一个函数和一个数字。

In your example, instead of the named function you are passing an anonymous function that simply does console.log('Task A'); 在您的示例中,您传递的是一个匿名函数,而不是命名函数,该函数只执行console.log('Task A'); and that function will be called every 10ms (approximately). 并且该函数将每10ms(大约)调用一次。

Create Your Own Function 创建自己的功能

 function setInterval() {
     console.log('Task A');
 }, 10);

This block of code is a Javascript Syntax Error and will not work. 此代码块是Javascript语法错误,将无法使用。 It looks like you're attempting to define your own function called setInterval() , but this is not the proper syntax for declaring a function. 似乎您正在尝试定义自己的名为setInterval()的函数,但这不是声明函数的正确语法。

You could make it legal syntax like this: 您可以使它成为合法的语法,如下所示:

function setInterval() {
    console.log('Task A');
}

And, then you would call it like this: 然后,您将这样称呼它:

 setInterval();

This has nothing to do with the previous example. 这与前面的示例无关。 This just creates a function that runs once each time you call it. 这只会创建一个函数,每次调用它一次。 If you actually gave it the same name as the global function setInterval() , then your local definition would replace it within the scope it was declared. 如果您实际给它的名称与全局函数setInterval() ,则您的本地定义将在声明的范围内替换它。

Your Other Questions 您的其他问题

Also, i'm a little confused what the '10' means at the end of the method, my guess is the time it takes for the method to complete? 另外,我有点困惑方法结尾处的“ 10”是什么意思,我猜是方法完成需要花费时间吗?

The 10 in the first example is the number of milliseconds for the interval timer. 第一个示例中的10是间隔计时器的毫秒数。 The 10 in the second example does not belong there - it's part of a Javascript syntax error. 第二个示例中的10不属于此处-它是Javascript语法错误的一部分。

Callbacks - Are Callbacks just technically another name in Node for testing code? 回调-回调在技术上是否只是Node中用于测试代码的另一个名称?

No. A callback is when a function takes an argument that is a function reference (eg the name of a function or an anonymous function). 否。回调是当函数接受作为函数引用的参数(例如,函数名称或匿名函数)时。 When you pass a callback to this function, you can expect that the function will call the callback one or more times at some time in the future. 将回调传递给此函数时,可以预期该函数将来会在某个时候调用一次或多次。 When exactly it is called or how many times it is called depends entirely upon what the function does and how it is written. 何时确切调用它或调用多少次完全取决于函数的功能和编写方式。 The term "callback" comes from the notion that this function will be "called back" some time in the future. 术语“回调”源自这样的观念,即将来某个时候该函数将被“回调”。

Is there a method I can use in the Node(CLI) to see all of the methods in a module? 我可以在Node(CLI)中使用一种方法来查看模块中的所有方法吗?

I'm not aware of a specific feature in the command line interface that will give you the methods of a module, but you can iterate them yourself or look at them in the debugger. 我不知道命令行界面中的特定功能会为您提供模块的方法,但是您可以自己对其进行迭代或在调试器中对其进行查看。

When you load a module in node with something like: 当您在节点中加载类似以下内容的模块时:

var fs = require('fs');

The object you get back from the require() function is a Javascript object. require()函数获得的对象是Javascript对象。 All the methods of that module are properties on that object. 该模块的所有方法都是该对象的属性。 You can inspect that object in the debugger or with console.log(fs) or by writing code to iterate the properties of that object. 您可以在调试器中或通过console.log(fs)或通过编写代码来迭代该对象的属性来检查该对象。

var fs = require('fs');
for (var prop in fs) {
    if (fs.hasOwnProperty(prop)) {
        console.log(prop);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM