-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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