Is there a way to achieve something similar to this?
var isChild = isInstanceOf( var1, 'Constructor')
so that it is equivalent to
var isChild = (var1 instanceof Constructor)
The challenge is that the function Constructor is not available in the current scope, so I am looking to pass a string instead.
I believe one approach could be to traverse up the prototype chain to obtain the constructor.toString()
and then compare it, but I am unsure of how to accomplish this.
--
Allow me to provide more context as I have come across a better solution
I encountered a circular reference between two function constructors, and when attempting to reference it, RequireJS continuously returned undefined. (In this scenario, Constructor would be undefined.)
I came across this helpful information: http://requirejs.org/docs/api.html#circular
Here is the snippet of code that caused the issue:
//(in BaseControl.js)
define(['src/utils/models/Field'],
function(Field) {
[...]
setField: function(field) {
if (!field instanceof Field) throw new Error('field should be an instance of Field');
[...]
The issue stemmed from the fact that Field also required BaseControl, resulting in Field being undefined, leading to the following error:
Uncaught TypeError: Expecting a function in instanceof check, but got false
I managed to resolve this by following the requireJS documentation:
define(['require', 'src/utils/models/Field'],
function(require, Field,) {
[...]
setField: function(field) {
if (!Field) Field = require('src/utils/models/Field');
if (!field instanceof Field) throw new Error('field should be an instance of Field');
[...]