2

I am new to js oop and i dont understand how i can do this.

i have

var Cadastro = Object.create(QForm);
var options = {
    myInstanceName: "Cadastro",
    dependentListsMode: "one",
    defaultButtons: ['enter-query', 'new']
}
Cadastro.initForm(options);

then i have QForm.js

var QForm;
QForm = {
    initForm: function (parms) {       
        $.extend(this, parms);        
        var frmObj = $(this.formId);
        this.userRestrictions(parms);       
        $(this.currentForm).find("a[data-form-action]").hide();
        this.clearForm();
        this.disableFields();
    },

The problem is that if i have e object in the same page , this.currentForm have the value of the latest intantiated object .

QForm.js is very extense file with lot of methods. How can i manage this. Thanks

6
  • 1
    Where are you setting this.currentForm? Commented Feb 5, 2016 at 18:30
  • You seem to be confused between objects, instances, and classes. Commented Feb 5, 2016 at 18:32
  • this.currentForm is in options, ive cut it because there was to much properties. Not confuse about objects or classes or instances. But Robusto you are right, qform is an object not a class Commented Feb 5, 2016 at 19:08
  • QForm is object literal and i have to use a constructor. thats it? Commented Feb 5, 2016 at 19:17
  • 1
    Also it would be good if you reproduce your issue on a small self-contained example, you can start with code from my answer and extend it just to show the problem. Commented Feb 6, 2016 at 0:52

1 Answer 1

2

In general your code works, it uses Object.create to create new instances based on the QForm prototype and new instances do not share the properties, here is a short working example:

var QForm;
QForm = {
    initForm: function (parms) {       
        $.extend(this, parms);        
        this.frmObj = $(this.formId);
    }
};

var cadastro = Object.create(QForm);
var options = {
    myInstanceName: "Cadastro",
    formId: "CadastroForm",
    dependentListsMode: "one",
    defaultButtons: ['enter-query', 'new']
}
cadastro.initForm(options);

var formTwo = Object.create(QForm);
var options = {
    myInstanceName: "FormTwo",
    formId: "test",
    dependentListsMode: "one",
    defaultButtons: ['enter-query', 'new']
}
formTwo.initForm(options); 

alert(cadastro.formId);
alert(formTwo.formId);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you run it, you will see CadastroForm and then test, so two instances created based on the QForm have different formId properties.

Sign up to request clarification or add additional context in comments.

3 Comments

Though it's hard to tell what is wrong from what the OP posted, this should be helpful
@LeonelMatiasDomingos check my comments to the answer and this link, if you want to get help with the specific problem, you should create the "Minimal, Complete, and Verifiable example".
@LeonelMatiasDomingos Once you have this example, just edit your question and put all the details and example into it, also check the whole 'asking' help section, there are some useful tips about asking good questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.