为什么我不能在 nowjs 中扩展每个人的口袋?
我正在尝试为每个人的 nowjs 的口袋
提供功能。我想通过_.extend
每个人的口袋,即everyone.now
来做到这一点。由于某种我无法理解的原因,_.extend
无法在客户端正确提供该功能。
这是我当前的代码:
var _ = require("underscore"),
everyone = require("nowjs").initialize(app);
everyone.now.foo = function() {};
_.extend(everyone.now, {
bar: function() {}
});
console.log(everyone.now.foo); // [Function]
console.log(everyone.now.bar); // undefined
在服务器端和客户端,我都可以很好地执行 now.foo() 。另一方面,由于 now.bar
未定义,now.bar()
失败。客户端和服务器端都是如此。我尝试检查服务器端是否存在,如上面最后一行所示。然而,这一行记录了undefined
。
Underscore 的 extend
函数(显然)确实适用于其他对象,所以我猜它与 nowjs 使用的“神奇名称空间”有关。
为什么扩展不适用于 everyone.now
以及如何让它工作?
编辑2:我对代理进行了更多研究。似乎通过传递变量来设置代理上的属性,因为它的名称不起作用。我删除了第一次编辑,因为这个测试用例的范围更窄。
为什么这不起作用?这是一个错误吗? (大多数时候我自己问这个问题,我知道不是,但这真的让我一无所知......)
var proxy = Proxy.create({
get: function(pr, name) {
console.log("get called");
return null;
},
set: function(pr, name, value) {
console.log("set called");
}
});
var key = "foo";
proxy["foo"] = "bar";
proxy[ key ] = "bar";
proxy["foo"];
proxy[ key ];
日志结果:
set called
get called
get called
显然, proxy[ key ] = "bar";
不导致在代理上调用 set
。这是为什么?
I'm trying to provide functions in everyone's pocket
of nowjs. I'd like to do so by _.extend
ing everyone's pocket, i.e. everyone.now
. For some reason which I cannot understand, _.extend
fails to properly provide the function at the client side.
This is my current code:
var _ = require("underscore"),
everyone = require("nowjs").initialize(app);
everyone.now.foo = function() {};
_.extend(everyone.now, {
bar: function() {}
});
console.log(everyone.now.foo); // [Function]
console.log(everyone.now.bar); // undefined
On both the server and client sides, I can do now.foo()
just fine. On the other hand, now.bar()
fails because now.bar
is not defined. This is the case on both the client and server sides. I tried to check for existence at the server side, as shown above on the last line. However, this line logs undefined
.
Underscore's extend
function (obviously) does work on other objects so I guess it has something to do with the "magical namespace" that nowjs uses.
How come extending doesn't work with everyone.now
and how can I get it to work?
Edit 2: I digged some more into proxies. It seems like setting a property on a proxy by passing a variable as its name does not work. I removed my first edit because this testcase is more narrowed down.
Why is this not working? Is this a bug? (Most of the times I ask this myself I know it isn't, but this is really making me clueless...)
var proxy = Proxy.create({
get: function(pr, name) {
console.log("get called");
return null;
},
set: function(pr, name, value) {
console.log("set called");
}
});
var key = "foo";
proxy["foo"] = "bar";
proxy[ key ] = "bar";
proxy["foo"];
proxy[ key ];
Log result:
set called
get called
get called
Apparently, proxy[ key ] = "bar";
does not cause set
to be called on the proxy. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们在 NowJS 网站上发布了一篇博客文章,介绍如何使用
node-proxy
,而不是使用--harmony_proxies
标志的本机 V8 实现。Node 当前使用的 V8 版本似乎包含多个有关代理的错误,这些错误导致了问题中概述的奇怪行为。然而,node-proxy 是一个启用代理(NowJS“神奇命名空间”的核心)的模块,没有这些错误。 (这些错误也在较新版本的 V8 中得到了修复,但这需要自定义构建 Node。)
我只是不知道如何在 Windows 上构建
node-proxy
(这是一个.node
插件;不是纯 JavaScript 插件)。在上面的博客文章中,他们分发了编译后的模块,现在一切都像魅力一样。修复方法:
node_modules
文件夹并将其重命名为now
编辑: Node 0.7.0 使用 V8 3.8.6 也解决了这个问题。只需使用
--harmony
标志运行并删除对node-proxy
的引用即可。They posted a blog entry on the NowJS website on how to use
node-proxy
on Windows, instead of the native V8 implementation using the--harmony_proxies
flag.It appeared that the V8 version that Node currently uses contains several bugs with regard to proxies, which were causing the weird behaviour as outlined in the question.
node-proxy
, however, is a module that enables proxies (the core of the "magical namespace" of NowJS) without those bugs. (The bugs are fixed in a newer version of V8 as well, but that would require a custom build of Node.)I just couldn't figure out how to build
node-proxy
on Windows (it's a.node
addon; not a pure JavaScript one). In the above blog post they distributed the compiled module, and everything now works like a charm.To fix:
node_modules
folder and rename it tonow
Edit: Node 0.7.0 uses V8 3.8.6 which also solves this issue. Just run with the
--harmony
flag and remove the reference tonode-proxy
.