I've been fiddling around a bit with the prototype and closure patterns in Javascript. As you might know, there's a performance penalty when using the closure pattern because it redefines the same functions for every instance of an object. However, the closure pattern does allow for private variables, which makes encapsulation easier. Here's a typical example of the prototype patt
我一直在Javascript中使用原型和闭包模式来摆弄一下。 正如你可能知道的那样,当使用闭包模式时会有性能损失,因为它为对象的每个实例重新定义了相同的函数。 然而,闭包模式确实允许私有变量,这使得封装更容易。 以下是原型模式的典型示例: function Foo(val) { this.val = val; } Foo.prototype.getVal = function() { return this.val; } var f = new Foo(42); 我在想,为什么你不能做这样的事情? functio
I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with new SomeFunction() , and when you would want to use one over the other. Consider the following example: var test = { val: 1, func: function() { return this.val; } }; var testA = Object.create(test); testA.val = 2; cons
我最近偶然发现了JavaScript中的Object.create()方法,并试图推断它与用new SomeFunction()创建对象的新实例的方式不同,以及何时想要使用另一个方法。 考虑下面的例子: var test = { val: 1, func: function() { return this.val; } }; var testA = Object.create(test); testA.val = 2; console.log(test.func()); // 1 console.log(testA.func()); // 2 console.log('other test'); var otherTes
I think I have misunderstood how Javascript prototypal inheritance works. Specifically, the prototypes internal variables seem to be shared between multiple different sub-objects. It is easiest to illustrate with code: var A = function() { var internal = 0; this.increment = function() { return ++internal; }; }; var B = function() {}; // inherit from A B.prototype = new A; x = new
我想我误解了Javascript原型继承的工作原理。 具体而言,原型内部变量似乎在多个不同的子对象之间共享。 用代码说明最简单: var A = function() { var internal = 0; this.increment = function() { return ++internal; }; }; var B = function() {}; // inherit from A B.prototype = new A; x = new B; y = new B; $('#hello').text(x.increment() + " - " + y.increment()); 这会输出1 - 2 (在JSBin上
What's the difference between var A = function () { this.x = function () { //do something }; }; and var A = function () { }; A.prototype.x = function () { //do something }; The examples have very different outcomes. Before looking at the differences, the following should be noted: A constructor's prototype provides a way to share methods and values among instan
有什么区别 var A = function () { this.x = function () { //do something }; }; 和 var A = function () { }; A.prototype.x = function () { //do something }; 这些例子的结果非常不同。 在查看差异之前,应该注意以下几点: 构造函数的原型提供了一种通过实例的private [[Prototype]]属性在实例之间共享方法和值的方法。 函数的这个由函数的调用方式或使用绑定来设置(这里没有讨论)。 在
The thing is: I need to play a streaming sound with HTML5 using audio tag, but the problem is that to access that sound I have to request a REST API that need an special header in the request. So the source audio is in: http://mydomain.com/aapi/source/1234 But it only works if I request it with some header like this: 'x-sk-apiKey' : 'APIKey', 'x-sk-version' : '1.0', 'x-sk-client' : 'iPhone', 'x
问题是:我需要使用音频标签来播放HTML5的流媒体声音,但问题在于,要访问声音,我必须请求需要特殊标头的REST API。 所以源音频是在:http://mydomain.com/aapi/source/1234但它只适用于我请求这样的头像: 'x-sk-apiKey' : 'APIKey', 'x-sk-version' : '1.0', 'x-sk-client' : 'iPhone', 'x-sk-device-id' : 'device-id' 'x-sk-device-id' : 'device-id' 'x-sk-token' : '12332424r3fwd23r2cdscadscsd234234' 是否可以将音
In my nearest future I will have to make a system with C++ backend and web frontend (requirements). At the moment, I don't know much more about it. I think that Frontend will be triggering data delivery, not backend - so no need for Comet-like things. Because of possibly little experience in this field, I'd really appreciate your comments about design decisions I made. First of all,
在我最近的将来,我将不得不使用C ++后端和web前端(需求)制作一个系统。 目前,我对此不甚了解。 我认为Frontend会触发数据传输,而不是后端 - 所以不需要Comet类的东西。 由于这方面可能经验不足,我非常感谢您对我所做的设计决定的评论。 首先,我不喜欢从C ++生成HTML的选项。 所以,C ++后端必须与Javascript前端进行通信。 我在这里看到的最简单的选项是Ajax。 到目前为止,我认为它应该没问题。 通过使用C ++
According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way. The problem with that example is that I don't have thousands of people typing the data in the database using the mongo console. Instead I am trying to use mongoose. So my schema looks something like this: var entitySchema = mongoose.Schema({ testvalue:{type:
根据这个MongoDB文章可以自动增加一个字段,我希望使用计数器收集方式。 这个例子的问题是,我没有成千上万的人使用mongo控制台在数据库中输入数据。 相反,我正在尝试使用猫鼬。 所以我的模式看起来像这样: var entitySchema = mongoose.Schema({ testvalue:{type:String,default:function getNextSequence() { console.log('what is this:',mongoose);//this is mongoose var ret = db.counters.findA
I have the follow structure: |server |db |mongooseTest.js |test |userModel.test.js |user |userModel.js With their code: mongooseTest.js var mongoose = require('mongoose'); module.exports = function() { var db = mongoose.createConnection('localhost', 'dbUnitTest'); db.on('connected', function() { console.log('DB: ' + db.name + ' local: ' + db.
我有以下结构: |server |db |mongooseTest.js |test |userModel.test.js |user |userModel.js 用他们的代码: mongooseTest.js var mongoose = require('mongoose'); module.exports = function() { var db = mongoose.createConnection('localhost', 'dbUnitTest'); db.on('connected', function() { console.log('DB: ' + db.name + ' local: ' + db.host + ':' + db.
I have the following: var express = require('express'), app = express.createServer(); app.get("/offline.manifest", function(req, res){ res.contentType("text/cache-manifest"); res.end("CACHE MANIFEST"); }); app.listen(8561); The network tab in Chrome says it's text/plain . Why isn't it setting the header? The code above works, my problems were caused by a linking to an old v
我有以下几点: var express = require('express'), app = express.createServer(); app.get("/offline.manifest", function(req, res){ res.contentType("text/cache-manifest"); res.end("CACHE MANIFEST"); }); app.listen(8561); Chrome中的网络标签显示它是text/plain 。 为什么不设置标题? 上面的代码工作正常,我的问题是由链接到旧版本的express-js引起的 res.type('json')现在也可以工作,正
For a new node.js project I'm working on, I'm thinking about switching over from a cookie based session approach (by this, I mean, storing an id to a key-value store containing user sessions in a user's browser) to a token-based session approach (no key-value store) using JSON Web Tokens (jwt). The project is a game that utilizes socket.io - having a token-based session would be use
对于我正在开发的一个新的node.js项目,我正在考虑从基于cookie的会话方法切换(我的意思是,将id存储到包含用户浏览器中的用户会话的键值存储)转换为使用JSON Web Tokens(jwt)的基于令牌的会话方法(无键值存储)。 该项目是一个利用socket.io的游戏 - 在一个会话中会有多个通信通道(web和socket.io)的情况下,基于令牌的会话将非常有用。 如何使用jwt方法从服务器提供令牌/会话失效? 我也想了解我应该用这种模式去