Skip to content

Instantly share code, notes, and snippets.

@Seminioni
Forked from wentout/isArrowOrFunctionOrClass.js
Created September 26, 2020 05:51
Show Gist options
  • Save Seminioni/7d5e6fe8eda7eb7e821a4a66bc61f2e6 to your computer and use it in GitHub Desktop.
Save Seminioni/7d5e6fe8eda7eb7e821a4a66bc61f2e6 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