一个简单的问题:我不明白是否存在在 's 左侧使用变量名的标准require(),如果存在,它需要什么:大写或小写(驼峰式)变量名?
更清楚地说:
myImage = require('./image');
Run Code Online (Sandbox Code Playgroud)
或者
MyImage = require('./image');
Run Code Online (Sandbox Code Playgroud)
?
我问,因为所需的模块总是返回对象,所以它们可以用作类或对象......
使用nodejs和打字稿查看另一个问题的另一个答案,我得到这个异常:
类型错误:interval$.zip 不是函数
我的代码(rxjs2.ts):
{
var Rx = require('rxjs');
const interval$ = Rx.interval(1000);
const items$ = Rx.from([1,2,3]);
const itemsOverTime$ = interval$.zip(items$).repeat();
itemsOverTime$.subscribe(([time, val]) => {
console.log(val);
// 1
// 2
// 3
// 1
// 2
// 3
});
}
Run Code Online (Sandbox Code Playgroud)
从 VSCode 控制台运行它,例如:node rxjs2.ts
我的 package-lock.json
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"@types/node": {
"version": "10.12.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.2.tgz",
"integrity": "sha512-53ElVDSnZeFUUFIYzI8WLQ25IhWzb6vbddNp8UHlXQyU0ET2RhV5zg0NfubzU7iNMh5bBXb0htCzfvrSVNgzaQ=="
},
"rxjs": {
"version": "6.3.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.3.3.tgz",
"integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==",
"requires": {
"tslib": "1.9.3"
}
},
"tslib": { …Run Code Online (Sandbox Code Playgroud) 新发布的草案在[expr.prim.req]/6 中提到:
如果将模板参数替换为需求总是会导致替换失败,则程序格式错误;无需诊断。[ 示例:
Run Code Online (Sandbox Code Playgroud)template<typename T> concept C = requires { new int[-(int)sizeof(T)]; // ill-formed, no diagnostic required };— 结束示例 ]
但是为什么我们不能保证诊断总是失败,而不是跳过诊断?
我正在尝试在服务器端使用 ES6。我将端点与服务器文件分开:
存档.js:
import { Router } from "express";
const router = Router();
router.get("/", async (req, res) => { "some code"});
export default router;
Run Code Online (Sandbox Code Playgroud)
当我想像这样将它导入我的服务器文件时:
import archive from "./endpoints/archive.js";
app.use("/archive", archive);
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
(节点:9565)实验警告:ESM 模块加载器是实验性的。internal/modules/run_main.js:54 internalBinding('errors').triggerUncaughtException()
有什么想法吗?我不想回到 require/module.exports
我看到很多 Ruby 程序员把代码放在require第一行,我想,他们这样做是有充分理由的,而不是编写可读的代码!特别是当他们使用多个...
现在我知道需要更多的文件和库会给内存/程序带来更多的负载,但是是否应该只在需要的时间/地点之前需要一些东西?或者在程序中将 require 放在更早的位置是否会在长代码中以某种方式带来好处/坏处?或者这取决于!
例如
require 'open-uri'
require 'rake'
require 'logic.rb'
code
code...
code..........
Run Code Online (Sandbox Code Playgroud)
或者
code
require 'open-uri'
require 'rake'
code...
require 'logic.rb'
code..........
Run Code Online (Sandbox Code Playgroud) include("somefile.php");include_once("somefile.php");require("somefile.php");require_once("somefile.php");这些有什么区别?
我有一个Perl类文件(包):person.pl
package person;
sub create {
my $this = {
name => undef,
email => undef
}
bless $this;
return $this;
}
1;
Run Code Online (Sandbox Code Playgroud)
我需要在另一个文件中使用这个类:test.pl
(请注意,person.pl和test.pl位于同一目录中)
require "person.pl";
$john_doe = person::create();
$john_doe->{name} = "John Doe";
$john_doe->{email} = "johndoe@example.com";
Run Code Online (Sandbox Code Playgroud)
但它没有取得成功.
我正在使用XAMPP来运行PHP和Perl.
我认为使用"require"获取类'person'的代码似乎不对,但我不知道如何解决这个问题.请帮忙...
这对谷歌来说非常困难.我有一个位于的文件
/folder_1/folder_2/folder_3/my_ruby_file_to_be_executed.rb
Run Code Online (Sandbox Code Playgroud)
在该文件中我需要这个文件
/folder_1/folder_4/file_to_require.rb
Run Code Online (Sandbox Code Playgroud)
我无法锻炼如何写出从一个到另一个的相对路径.
我正在使用红宝石1.93
我的设置如下:
server.js 要求 utils.jsutils.js 将数据从mongodb加载到内存并导出它server.js使用utils.js导出的变量我担心的问题是mongodb调用是异步的.utils.js在mongodb调用完成之前返回,这意味着server.js在require之后继续执行时将使用未定义的变量.
解决这个问题的最佳方法是什么?我唯一能想到的是将我的server.js代码包装在一个巨大的回调中,并将其传递给进行mongodb调用的函数.对我来说似乎有点乱,有没有更好的方法呢?
码:
server.js
var utils = require("./modules/utils.js");
console.log(utils);
//Do whatever
utils.js
var mods = [];
var db = require("mongojs").connect("localhost", ["modules"]);
db.modules.find({}, function(err, modules){
mods = modules;
});
module.exports = mods;
Run Code Online (Sandbox Code Playgroud) 在MyModule文件夹中,我有两个JS文件.
SayHello.js
module.exports.SayHello = function() {
return('Hello !');
}
Run Code Online (Sandbox Code Playgroud)
SayByeBye.js
module.exports.SayByeBye = function() {
return('Bye Bye!');
}
Run Code Online (Sandbox Code Playgroud)
在Node.js中,我想要MyModule文件夹中的所有文件并调用函数SayHello&SayByeBye直接类似于:
require(./MyModule)
console.log(SayHello());
console.log(SayByeBye());
Run Code Online (Sandbox Code Playgroud)
编辑:
回答@Yanick Rochon,我这样做:
> ./app/my-module/index.js
global.SayHello = require('./my-module/SayHello').SayHello;
global.SayByeBye = require('./my-module/SayByeBye').SayByeBye;
Run Code Online (Sandbox Code Playgroud)
> ./app/my-module/say-hello.js
module.exports.SayHello = function() {
return('Hello !');
};
Run Code Online (Sandbox Code Playgroud)
> ./app/my-module/say-byebye.js
module.exports.SayByeBye = function() {
return('Bye Bye !');
};
Run Code Online (Sandbox Code Playgroud)
> ./app/main.js
require('./my-module');
console.log(SayHello());
console.log(SayByeBye());
Run Code Online (Sandbox Code Playgroud)
节点文档中有关于全局对象的部分.
但是,应谨慎使用全局变量.通过向全局空间添加模块,我降低了可测试性和封装.但在这种情况下,我认为使用这种方法是可以接受的.
require ×10
node.js ×5
include ×2
javascript ×2
ruby ×2
asynchronous ×1
c++ ×1
c++-concepts ×1
c++20 ×1
class ×1
directory ×1
ecmascript-6 ×1
import ×1
oop ×1
performance ×1
perl ×1
php ×1
rxjs ×1
scripting ×1
templates ×1