Skip to content

Instantly share code, notes, and snippets.

View ufocoder's full-sized avatar
👽
🛸

Sergey ufocoder

👽
🛸
View GitHub Profile
function isNan(value) {
return value !== value;
}
Number.isNaN = Number.isNaN || (function(value) {
return value !== value;
})
function isNan(value) {
return Object.is(value, Number.NaN);
}
// Метод #1: свойство constructor
// Ненадежный метод
function isArray(value) {
return typeof value == 'object' && value.constructor === Array;
}
// Метод #2: instanceof
// Ненадежный метод в связи с возможностью изменения прототипа объекта
// Непредвиденные результаты при работе с `iframe`
function isArray(value) {
function type(value) {
var regex = /^\[object (\S+?)\]$/;
var matches = Object.prototype.toString.call(value).match(regex) || [];
return (matches[1] || 'undefined').toLowerCase();
}
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"
// объект-обертка: 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);
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 {
function handleError (err) { /* ... */ }
function handleSuccess (x) { /* ... */ }
function nodeCallback (err, thingIWant) {
if (err) {
handleError(err);
} else {
handleSuccess(thingIWant);
}
}
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" });