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

  • SEARCH
  • Home
  • 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 6367555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:33:46+00:00 2026-05-25T00:33:46+00:00

I have that function: function(stringsVar) { var stringRes = stringsVar || localize_en; if(‘window.’+stringsVar ===

  • 0

I have that function:

function(stringsVar) {
var stringRes = stringsVar || localize_en;
if('window.'+stringsVar === undefined) {
    stringRes = localize_en;
}
...
}

and doesn’t work. That was like that actually:

function(stringsVar) {
    var stringRes = stringsVar || localize_en;
}

that function can take a parameter or not and the code above is checking it correctly. Parameter of that function will be a variable. I want to add that ability to my function. It will check whether that variable is defined or not. If not there is a defined variable at system, localize_en, it will be assigned as default.

How can I correct my code. The second part of my code will be that functionality:
i.e stringsVar is localize_ar and it is not a defined variable (I define that kind of variables with var keyword)

if(window.localize_ar === undefined){
alert('yes');}
else {
alert('no');
}

I will add that functionality as parametric.

Any ideas?

PS: localize_en and something like that variables are object.

EDIT: I am working on JQuery localizer plugin => source code.
I call it as

$('html').localize('localize_' + tr);

However it can not understand it as an object, it works as if I do:

$('html').localize(localize_tr);

It changes it into a string maybe the problem lays on there?

  • 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-05-25T00:33:47+00:00Added an answer on May 25, 2026 at 12:33 am

    You can use the square bracket notation to refer to object members whose name is stored in a variable, so you’re probably looking for this:

    if (window[stringsVar] === undefined) {
    
    }
    

    Furthermore, the || operator will return the first truthy; what happens if an object is passed as the first parameter? That’s truthy, but you specifically want a string, so whilst the || operator looks cool, you might find the following more appropiate:

    if (typeof stringVar !== "string") {
        stringVar = "localize_en";
    }
    

    It also looks like you’re getting confused when to use a string to refer to the object your targeting, and when not to.

    When you going to be doing something like:

    window[someVar]
    

    someVar needs to be a string.

    It is possible to pass an object by reference in JavaScript, and after writing all the above to help you fix the problem you’ve currently got, a better approach will be to pass the object by reference in the first place and avoid the problem completely, rather than passing the name of the variable storing the object:

    function(obj) {
        if (typeof obj !== "object") { 
            obj = localize_en; // here we're wanting the object itself, rather than the name of the object, so we're not using a string.
        };
    
        // Now use `obj`. It'll be either the object the user passed, or the default (localize_en).
    
        // You can even store this in a global variable if you want to:
        window.selected_obj = obj;
    }
    

    Edit:

    From your comment, try this:

    function (stringsVar) {
        if (typeof stringsVar !== "string" || typeof window[stringsVar] !== "object") {
            stringsVar = "localize_en"; // Set the default of the argument, if either none is provided, or it isn't a string, or it doesn't point to a valid object
        }
    
        var stringRes = window[stringsVar];
    
        // Now do *whatever* you want with stringRes. It will either be the *valid* localization type the parameter specified, or the default ("localize_en").
    }
    

    You should pass this function a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that I use called sqlf(), it emulates prepared statements. For
I have a function that is effectively a replacement for print, and I want
I have a function that takes a struct, and I'm trying to store its
I have a function that automatically exports a table into a CSV file, then
I have a function that checks if a cookie (by name) exists or not:
I have a function that resembles the one below. I'm not sure how to

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.