我目前正在尝试为我的网站实施第三方身份验证功能.这是我从网上获得的"enableCORS"功能.它位于"Server.js"中.
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
var express = require("Express");
var url = require("url");
var http = require("http");
var app;
var port = process.argv[2];
app = express();
http.createServer(app).listen(port);
app.use(enableCORS)
Run Code Online (Sandbox Code Playgroud)
从我的网站,我发送一个httpGet请求到我的Server.js(我使用node -harmony server.js 3000在我的控制台中运行).这是httpGet函数:
function httpGet(URL) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", URL, false);
xmlHttp.send(null);
return xmlHttp.response;
}
Run Code Online (Sandbox Code Playgroud)
我发送的请求是:
var items = JSON.parse(httpGet("http://localhost:3000/items"));
Run Code Online (Sandbox Code Playgroud)
这很好用,我的控制台说GET请求是成功的200代码.这个工作的唯一原因是因为我使用"enableCORS"功能. …
在 JavaScript(或 TypeScript)中,对象是通过引用传递的,这与在函数中复制的原语不同。因此,是不是这样:
sum(one: number, two: number): number {
return one + two;
}
Run Code Online (Sandbox Code Playgroud)
总是比这效率低?
sum(input: { one: number, two: number}): number {
return input.one + input.two;
}
Run Code Online (Sandbox Code Playgroud)
在第一个函数中,'one' 和 'two' 被复制。在第二个函数中,'one' 和 'two' 只是对封装在对象中的原始 'one' 和 'two' 的引用,因此不会进行复制,这样可以节省计算量吗?
当然,如果我需要操纵 'one' 和 'two' 的值并且不希望更改持续存在,我会使用第二个函数。
因此,对于 JavaScript 开发人员来说,将他们的函数参数原语封装在一个对象中(假设他们不希望操作持续存在)不应该始终是建议的最佳实践吗?