Skip to content

Instantly share code, notes, and snippets.

View ufocoder's full-sized avatar
👽
🛸

Sergey ufocoder

👽
🛸
View GitHub Profile
function rememberX (x) {
return function getX () {
return x;
};
}
// ES6 version might look like the following
// const rememberX = x => () => x;
const get42 = rememberX(42);
// Проблема #1:
// Этот код вызовет ошибку,
// потому что `handleSuccess` / `handleError` не имеют доступа до `res`
function handleSuccess (data) {
res.writeHead(200, { "Content-Type": "application/octet-stream" }); // *Бум!*
res.write(data);
res.end();
}
function handleError (err) { /* ... */ } // *Бум!*
router.get("/some-file", function (req, res) {
const url = "./data/some-file.ext";
function handleError (err) {
res.writeHead(404);
res.end();
}
function handleSuccess (file) {
res.writeHead(200, { "Content-Type": "application/octet-stream" });
function handleError (err) { /* ... */ }
function handleSuccess (x) { /* ... */ }
function nodeCallback (err, thingIWant) {
if (err) {
handleError(err);
} else {
handleSuccess(thingIWant);
}
}
import { readFile } from "fs";
router.get("/some-file", function (req, res) {
const url = "./data/some-file.ext";
readFile(url, function (err, file) {
if (err) {
res.writeHead(404);
res.end();
} else {
// объект-обертка: new String("Hello World!")
(new String("Hello World!")).toLowerCase();
// объект-обертка: new String("Another String")
(new String("Another String"))[8];
// объект-обертка: new Number(53.12345)
(new Number(53.12345)).toFixed(2);
console.log(type('')); // "string"
console.log(type('hello')); // "string"
console.log(type(String('hello'))); // "string"
console.log(type(new String('hello'))); // "string"
console.log(type(0)); // "number"
console.log(type(-0)); // "number"
console.log(type(0xff)); // "number"
console.log(type(-3.142)); // "number"
console.log(type(Infinity)); // "number"
function type(value) {
var regex = /^\[object (\S+?)\]$/;
var matches = Object.prototype.toString.call(value).match(regex) || [];
return (matches[1] || 'undefined').toLowerCase();
}
// Метод #1: свойство constructor
// Ненадежный метод
function isArray(value) {
return typeof value == 'object' && value.constructor === Array;
}
// Метод #2: instanceof
// Ненадежный метод в связи с возможностью изменения прототипа объекта
// Непредвиденные результаты при работе с `iframe`
function isArray(value) {
function isNan(value) {
return Object.is(value, Number.NaN);
}