Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:17:15+00:00 2026-06-17T22:17:15+00:00

i have some javascript class someClass and have initialization function init which check constructor

  • 0

i have some javascript class someClass and have initialization function init which check constructor params. Object with this params are rather huge and this function became too large.

function SomeClass (paramObj) {
    this.inputHourId;
    this.inputHourName;
    this.inputHourValue;
    ......
    ......
    this.Init = function (paramObj) {
        if (typeof paramObj.hourOptions.inputName == "undefined") {
            this.inputHourName = "default_name";
        } else {
            this.inputHourName = paramObj.hourOptions.inputName;
        }

        if (typeof paramObj.hourOptions.inputValue == "undefined") {
            this.inputHourValue = "default_value";
        } else {
            this.inputHourValue = paramObj.hourOptions.inputValue;
        }
        ......
        ......
        ......
    }
    this.Init(paramObj);
}

To avoid code duplication and make it more readable i decide to create function which will do var initialization with check

this.initVar = function (veriable, value) {
    if (typeof value == "undefined") {
        veriable = "some_default_value";
    } else {
        veriable = val;
    }
}

after adding initVar function, my Init function should look like that:

this.Init = function (paramObj) {
    // Input hour initialization
    this.initVar(this.inputHourId, paramObj.hourOptions.inputId);
    this.initVar(this.inputHourName, 
paramObj.hourOptions.inputName);
    this.initVar(this.inputHourValue, paramObj.hourOptions.inputValue);
    ....
    ....
    ....
}

but after that class vars this.inputHourName are still undefined

Now the question. How can I init my class property with the help of function? Or how can i transmit class property like function parametr?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:17:17+00:00Added an answer on June 17, 2026 at 10:17 pm

    That’s because when you pass this.inputHourId to initVar() it will make a copy of the variable when it’s being updated; that’s not what you want.

    You can rewrite initVar() to this; it uses the name of the variable as a string rather than the variable itself. Then it uses this[variable] to update the actual instance variable.

    this.initVar = function (variable, value) {
        if (typeof value == "undefined") {
            this[variable] = "some_default_value";
        } else {
            this[variable] = val;
        }
    }
    

    Then, you call it like:

    this.initVar('inputHourId', paramObj.hourOptions.inputId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript class where pass options object in init function. this options
If I have a javascript class that runs some initialization code, it seems logical
I´m newbee in javascript and have some headaches.. I note that the this object,
If I have a javascript class which cannot be instantiated what should the constructor
I have a Person javascript class like function Person(_name, _id, _salary){ this.Name = _name;
Hi i have code like this: <span class=someclass style=display:none></span> Some text how to match
I have some javascript that goes out and fetches a javascript class on another
I have a javascript interface class to receive some values from javascript. I toasted
I have some javascript applying changes to some elements and a function loading more
I have some Javascript attached to the Service Activity form which fires OnLoad. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.