我该如何做这项工作:
class TestClass {
doMethod1 (arg1, arg2, cb)
{
this.doMethod2(arg1, arg2, function (result){cb (result)});
}
doMethod2 (arg1, arg2, cb) {
this.doMethod3(arg1, arg2, function(result){cb (result)});
}
doMethod3 (arg1, arg2, cb) {
var result = arg1 + arg2;
cb(result);
}
}
Run Code Online (Sandbox Code Playgroud)
test = new TestClass;
test.doMethod3(1,1,cb); test.doMethod2(1,1,CB);
两者都有效.
test.doMethod1(1,1,CB);
编辑:实际上,它确实有效.
我使用"胖箭头"语法解决了相关的词法范围问题:
doMethod1 (arg1, arg2, cb)
{
this.doMethod2(arg1, arg2, (result) => {cb (result)});
}
Run Code Online (Sandbox Code Playgroud)
确保doMethod1中的"this"与匿名回调函数中的"this"相同.
我想编写一个可在node.js服务器和浏览器之间重用的可移植模块.
这是阻止我的模块化事物.我正在阅读http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/,它看起来很简单,但是让tsc生成这样的东西可能吗?
我正在尝试让dnsmasq作为Docker容器内的DHCP服务器运行,向主机物理网络上的机器发出DHCP地址.我正在使用https://hub.docker.com/r/andyshinn/dnsmasq/上的Alpine Linux 6MB容器.
它可以作为主机端口53上的DNS服务器正常工作,但是没有任何东西监听端口67/udp,这是我期待DHCP的地方.我用
dhcping 192.168.2.2,但得到"没有回答".telnet 192.168.2.2 67返回"拒绝连接".
我在容器中的dnsmasq.conf文件如下所示:
interface=eth0
user=root
domain-needed
bogus-priv
no-resolv
local=/mydomain.io/
no-poll
server=8.8.8.8
server=8.8.4.4
no-hosts
addn-hosts=/etc/dnsmasq_static_hosts.conf
expand-hosts
domain=mydomain.io
dhcp-range=192.168.2.10,192.168.2.250,255.255.255.0,192.168.2.255,5m
# Have windows machine release on shutdown
dhcp-option=vendor:MSFT,2,1i
# No default route
dhcp-option=3
Run Code Online (Sandbox Code Playgroud)
主机的静态地址为192.168.2.2.
我像这样启动容器:
docker run -d --name dns -p 192.168.2.2:67:67/udp -p 192.168.2.2:53:53/udp sitapati/dns
此机器上没有防火墙,运行Ubuntu 16.04.
我曾经想过/尝试的事情:
docker inspect告诉我在桥接界面上它是172.17.0.2)--net host吗?我试过了,它仍然无法正常工作.这是在 TypeScript 4.0.2 中。类型系统可以从下面示例中的类型签名中推断出一些值。我很困惑为什么它不能推断const b.
谁能解释为什么,以及我如何编写可以的类型签名?
declare function identityA<T extends string[]>(p: readonly [...T]): T
declare function identityB<T extends any[]>(p: readonly [...T]): [...T]
const a = identityA(['r', 's']) // ['r', 's']
const b = identityB([...a, 3]) // ['r', 's', number]
Run Code Online (Sandbox Code Playgroud)