像q这样的promise/defer库是如何实现的?我试图阅读源代码,但发现它很难理解,所以我认为如果有人能从高层次向我解释在单线程JS环境中用于实现promise的技术是多么好像Node和浏览器.
我正在阅读传入asyncio包的源代码.请注意,在方法的最后,有一个self = None声明.它有什么作用?
def _run(self):
try:
self._callback(*self._args)
except Exception as exc:
msg = 'Exception in callback {}{!r}'.format(self._callback,
self._args)
self._loop.call_exception_handler({
'message': msg,
'exception': exc,
'handle': self,
})
self = None # Needed to break cycles when an exception occurs.
Run Code Online (Sandbox Code Playgroud)
我认为它会删除实例,但以下测试不建议如此:
class K:
def haha(self):
self = None
a = K()
a.haha()
print(a) # a is still an instance
Run Code Online (Sandbox Code Playgroud) 我有这个布局html:
<body ng-controller="MainController">
<div id="terminal"></div>
<div ng-view></div>
<!-- including scripts -->
</body>
Run Code Online (Sandbox Code Playgroud)
显然,当我尝试使用它$routeParams时MainController,它总是空的.重要的是要注意,MainController应该在每个可能的路线中生效; 因此我没有在我的app.js中定义它.我的意思是,我不是在这里定义它:
$routeProvider.when("/view1", {
templateUrl: "partials/partial1.html"
controller: "MyCtrl1"
})
$routeProvider.when("/view2", {
templateUrl: "partials/partial2.html"
controller: "MyCtrl2"
})
// I'm not defining MainController here!!
Run Code Online (Sandbox Code Playgroud)
事实上,我认为我的问题与此问题完全相同:https://groups.google.com/forum/#!topic/angular/ib2wHQozeNE
但是,我仍然没有得到如何在我的主控制器中获取路由参数...
编辑:
我的意思是我没有将我的MainController与任何特定路线相关联.它的定义; 它是所有其他控制器的父控制器.我想知道的是,当你去一个像/whatever路由匹配的URL时/:whatever,为什么只有子控制器能够访问路由参数,而主控制器不是?如何:whatever在主控制器中获取route参数?
考虑这段代码,每行末尾有控制台输出:
function whatever() {
console.log(arguments) // { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
console.log(Array.prototype.slice.call(arguments)) // [ 1, 2, 3, 4, 5 ]
console.log(Array.prototype.slice.call({ '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 })) // []
}
whatever(1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)
为什么第三个console.log输出一个空数组呢?
甚至可以使用指针而不是数组来实现二进制堆?我搜索了互联网(包括SO),但没有找到答案.
这里的主要问题是,如何跟踪最后一个指针?当您将X插入堆中时,将X放在最后一个指针处,然后将其冒泡.现在,最后一个指针指向哪里?
而且,当您想要删除根时会发生什么?您将根与最后一个元素交换,然后向下冒泡新根.现在,您如何知道再次删除root时所需的新"最后一个元素"是什么?
我正在做一些非常简单的事情。这是我的Dockerfile:
FROM alpine:latest
ADD hello.sh /bin/hello
RUN chmod +x /bin/hello
CMD /bin/hello
Run Code Online (Sandbox Code Playgroud)
然后我构建图像:
docker build -t hello .
Run Code Online (Sandbox Code Playgroud)
然后我运行图像:
docker run hello
Run Code Online (Sandbox Code Playgroud)
这是输出:
/bin/sh: /bin/hello: not found
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?如果我运行:
docker run hello cat /bin/hello
Run Code Online (Sandbox Code Playgroud)
我可以看到我的脚本内容,这让我更加困惑。
这篇CoffeeScript:
for i in [1..10]
console.log i
Run Code Online (Sandbox Code Playgroud)
编译为:
for (i = _i = 1; _i <= 10; i = ++_i) {
console.log(i);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它不只是使用i.有任何想法吗?
我正在尝试测试WebRTC应用程序.现在,我能做的最好的事情就是打开几个私人浏览器窗口,让它们互相交谈,但这显然无法扩展.
我正在寻找一种在一台机器上创建大量对等体的方法.我正在研究Phantom.js,但它似乎还没有支持WebRTC.有什么建议?
我试图了解使用抽象基类的好处.考虑这两段代码:
抽象基类:
from abc import ABCMeta, abstractmethod, abstractproperty
class CanFly:
__metaclass__ = ABCMeta
@abstractmethod
def fly(self):
pass
@abstractproperty
def speed(self):
pass
class Bird(CanFly):
def __init__(self):
self.name = 'flappy'
@property
def speed(self):
return 1
def fly(self):
print('fly')
b = Bird()
print(isinstance(b, CanFly)) # True
print(issubclass(Bird, CanFly)) # True
Run Code Online (Sandbox Code Playgroud)
简单继承:
class CanFly(object):
def fly(self):
raise NotImplementedError
@property
def speed(self):
raise NotImplementedError()
class Bird(CanFly):
@property
def speed(self):
return 1
def fly(self):
print('fly')
b = Bird()
print(isinstance(b, CanFly)) # True
print(issubclass(Bird, CanFly)) …Run Code Online (Sandbox Code Playgroud) javascript ×4
python ×2
algorithm ×1
angularjs ×1
binary-tree ×1
c ×1
c++ ×1
coffeescript ×1
docker ×1
node.js ×1
phantomjs ×1
pointers ×1
promise ×1
webrtc ×1