3

I am working in JavaScript where I need to check the instanceof a custom JavaScript object & if the instance of that object is customTypeA then I need perform certain functions, where as in all other cases, we need to perform some different set of logic.

More detailed scenario:

We have generic form submit button which gets included on all form pages. Now we have a common JavaScript for form submit and specific JavaScript files for each form pages. Each of these specific JavaScript files create an object with name of commonObjectName. Where on form submit JavaScript calls validate & submit on commonObjectName, which will in turn invoke validate & submit for the respective JavaScript instance.

Now, when I need to perform certain checks between validate & submit actions for form A, where as they are not needed for form B, so I wrote below code in formSubmit.js

var commonObjInstanceOfFormA = commonObjectName instanceof FormAJavascript;
if(commonObjInstanceOffFormA) {
    //do something
} else {
   //do something else
}

Now, the problem occurs when I am on Form B. FormAJavascript type gives a reference error since it is not included on form B at all.

Is there a way to find the type of contructor of commonObjectName in a string format or find the instance of the object in efficient way so that I can perform a different set of logic for type A & different for Type B?

2

3 Answers 3

1

Why not check for it with good old typeof commonObjectName != 'undefined'? It will do the check first and only if it succeeds in finding commonObjectName it will then try to match it by type, then repeat same for objectB on formB.

var commonObjInstanceOfFormA = typeof commonObjectName != 'undefined' &&  commonObjectName instanceof FormAJavascript;
var commonObjInstanceOfFormB = typeof commonObjectName != 'undefined' &&  commonObjectName instanceof FormBJavascript;
if(commonObjInstanceOffFormA) {
    //do something
} else {
   //do something else
}
Sign up to request clarification or add additional context in comments.

1 Comment

commonObjectName will be defined on all forms, so type of that variable is never going to be undefined unless it is not a form page at all. However, thanks for response.
0

Declare any types you subsequently want to test against whilst preserving them if they exist:

var FormAJavascript = FormAJavascript || null;

Test with:

if (FormAJavascript != null && commonObjectName instanceof FormAJavascript)

2 Comments

This approach also gives ReferenceError since FormAJavascript is never imported & page doesn't know about its existence.
It won't because its declared with var - jsbin.com/moroqi/1/edit?js,output
0

Finally I ended up using my form page id to confirm which form this is, I am trying to get formId in javascript & asserting it against string 'formA'. If assertion succeeds, then it means it is form A.

I would still like to have an elegant way of finding the form, instead of checking for form id on every single form. But atleast this works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.