Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active September 26, 2020 05:51
Show Gist options
  • Save wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba to your computer and use it in GitHub Desktop.
Save wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba to your computer and use it in GitHub Desktop.
The answer to the question is something an Arrow or Class or regular Function
'use strict';
const myArrow = () => {};
const myFn = function () {};
class MyClass {};
const isArrowFunction = (fn) => {
if (typeof fn !== 'function') {
return false;
}
if (fn.prototype !== undefined) {
return false;
}
return true;
};
const isRegularFunction = (fn) => {
if (typeof fn !== 'function') {
return false;
}
if (typeof fn.prototype !== 'object') {
return false;
}
if (fn.prototype.constructor !== fn) {
return false;
}
return Object.getOwnPropertyDescriptor(fn, 'prototype').writable === true;
};
const isClass = (fn) => {
if (typeof fn !== 'function') {
return false;
}
if (typeof fn.prototype !== 'object') {
return false;
}
if (fn.prototype.constructor !== fn) {
return false;
}
return Object.getOwnPropertyDescriptor(fn, 'prototype').writable === false;
};
console.log('--- arrow ---')
console.log('is arrow : ', isArrowFunction(myArrow)); // true
console.log('function : ', isRegularFunction(myArrow)); // false
console.log('is class : ', isClass(myArrow)); // false
console.log('--- function ---')
console.log('is arrow : ', isArrowFunction(myFn)); // false
console.log('function : ', isRegularFunction(myFn)); // true
console.log('is class : ', isClass(myFn)); // false
console.log('--- class ---')
console.log('is arrow : ', isArrowFunction(MyClass)); // false
console.log('function : ', isRegularFunction(MyClass)); // false
console.log('is class : ', isClass(MyClass)); // true
debugger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment