这是我的数据:
[
{
url: 'www.example.com/hello',
id: "22"
},
{
url: 'www.example.com/hello',
id: "22"
},
{
url: 'www.example.com/hello-how-are-you',
id: "23"
},
{
url: 'www.example.com/i-like-cats',
id: "24"
},
{
url: 'www.example.com/i-like-pie',
id: "25"
}
]
Run Code Online (Sandbox Code Playgroud)
使用Lodash,我怎么能删除具有重复id键的对象?有过滤器,地图和独特的东西,但不太确定.
我的真实数据集要大得多,并且有更多的密钥,但概念应该是相同的.
在JavaScript中,这两个示例之间的区别是什么:
先决条件:
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
Run Code Online (Sandbox Code Playgroud)
继承示例A使用Object.create:
function MyClass(){
}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
Run Code Online (Sandbox Code Playgroud)
继承示例B使用new关键字
function MyClass(){
}
MyClass.prototype = new SomeBaseClass();
Run Code Online (Sandbox Code Playgroud)
这两个例子似乎做同样的事情.你什么时候选择一个而不是另一个?
另一个问题:考虑下面链接中的代码(第15行),其中对函数自身构造函数的引用存储在原型中.为什么这有用?
https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js
摘录(如果您不想打开链接):
THREE.ImageLoader.prototype = {
constructor: THREE.ImageLoader
}
Run Code Online (Sandbox Code Playgroud) 对于某些页面,我在我的应用程序中有自定义的500,404和403错误处理.因此,例如在数据库查询失败后,我会去:
return next({status: 404, message: 'Record not found'});
Run Code Online (Sandbox Code Playgroud)
要么
return next(new Error('Bad things have happened')});
Run Code Online (Sandbox Code Playgroud)
在我的中间件中,我有一个错误处理程序:
app.use(function (err, req, res, next) {
// handle error
});
Run Code Online (Sandbox Code Playgroud)
问题是永远不会调用错误处理程序,而是将错误调用堆栈打印到浏览器中.我希望处理程序呈现自定义错误页面.
app.js
var express = require('express')
, app = express()
, swig = require('swig')
, config = require('./lib/config')
, env = process.env.NODE_ENV || 'development'
, path = require('path');
config.configure(env);
app.engine('html', swig.renderFile);
app.set('view cache', false);
swig.setDefaults({
cache: config.get('swigCache')
});
app.set('view engine', 'html');
app.set('views', __dirname + '/lib/views');
require('./lib/util/swig');
require('./lib/initialisers/mongodb')();
require('./lib/initialisers/aws')();
require('./lib/middleware')(app); // first load middleware
require('./lib/routes')(app); …
Run Code Online (Sandbox Code Playgroud) 我在应用程序加载大量URL时遇到控制流问题.我正在使用Caolan Async和NPM请求模块.
我的问题是,只要将函数添加到队列,HTTP响应就会启动.理想情况下,我想构建我的队列,并且只在队列启动时才开始发出HTTP请求.否则回调会在队列启动之前开始触发 - 导致队列过早完成.
var request = require('request') // https://www.npmjs.com/package/request
, async = require('async'); // https://www.npmjs.com/package/async
var myLoaderQueue = []; // passed to async.parallel
var myUrls = ['http://...', 'http://...', 'http://...'] // 1000+ urls here
for(var i = 0; i < myUrls.length; i++){
myLoaderQueue.push(function(callback){
// Async http request
request(myUrls[i], function(error, response, html) {
// Some processing is happening here before the callback is invoked
callback(error, html);
});
});
}
// The loader queue has been made, now start to process …
Run Code Online (Sandbox Code Playgroud) 作为一名整天进行上下文切换的开发人员,我想保存打开的选项卡组并在以后恢复它们(在同一项目窗口中)。
例如,当我被要求“处理功能 A”时,我想打开该项目与“功能 A”相关的所有选项卡。
我花了太多时间搜索文件关闭、打开选项卡并沿着选项卡栏滚动以找到我需要的内容。
一天内处理 5 个不同的功能,我需要快速切换编辑器以反映我正在处理的内容。
是否有任何内置功能或扩展?
如何在EC2实例前设置Cloudfront?我有兴趣让用户点击Cloudfront网址而不是EC2源.
因此,用户不会点击ec2-52-64-xxx-xxx.ap-southeast-2.compute.amazonaws.com,而是点击d111111abcdef8.cloudfront.net.
我的目的是通过减少EC2实例的流量和CPU负载来节省托管费用,同时为海外用户提供更快的加载时间.
我只是将我的DNS指向Cloudfront网址而不是EC2源吗?
我们看到这种方法一直在使用:
(function (window) {
var document = window.document,
location = window.location,
navigator = window.navigator;
})(window)
Run Code Online (Sandbox Code Playgroud)
在研究上面的代码片段时,我想知道为什么像全局可访问的对象window
作为参数传递给函数.可能是这样的:
你怎么看?
摘自 CircleCI 配置文件:
deploy:
machine:
enabled: true
steps:
- run:
name: AWS EC2 deploy
command: |
ssh -o "StrictHostKeyChecking no" ubuntu@xxx.xxx.xxx.xxx "cd ~/circleci-aws; git pull; npm i; npm run build; pm2 restart build/server
Run Code Online (Sandbox Code Playgroud)
如何将命令分成多行?尝试了以下语法,但它只运行第一个命令:
deploy:
machine:
enabled: true
steps:
- run:
name: Deploy
command: |
ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx
cd ~/circleci-aws
git pull
npm i
npm run build
pm2 restart build/server
Run Code Online (Sandbox Code Playgroud) 我注意到,每当我停靠浏览器窗口或切换标签时,requestAnimationFrame都会停止被调用(我预计会发生这种情况).
有没有办法检测何时发生这种停止?
原因是,我在游戏中运行了计时器.我想在requestAnimationFrame不再渲染时停止计时器.
我查看了'window.blur'和'window.focus'事件,但这些与requestAnimationFrame何时停止和启动无关(例如,当您在浏览器窗口外单击时,会触发window.blur事件但requestAnimationFrame保持不变运行).
我想在requestAnimationFrame启动和停止时订阅.你知道吗?
在 Node.js Express 应用程序中,有没有办法在将所有 Handlebars 模板发送到渲染器之前缩小它们?
我考虑过创建一个 Express 中间件来对 HTML 响应主体进行缩小 - 但我很快意识到这是非常低效的,因为缩小会发生在每个 HTTP 请求上。
必须有一种缩小 .hbs 模板并在服务器端缓存它们的方法吗?
javascript ×4
node.js ×3
express ×2
asynchronous ×1
circleci ×1
circleci-2.0 ×1
inheritance ×1
lodash ×1
middleware ×1
scope ×1
ssh ×1
unix ×1
window ×1