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 8493393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:59:37+00:00 2026-06-10T22:59:37+00:00

Trying to create a class in JavaScript that can both have a function at

  • 0

Trying to create a “class” in JavaScript that can both have a function at the root of the class and other sub functions:

Validate(word) – returns true or false if the word is validated

Validate.getRule() – returns the rules used to validate the word.

Here is example code:

var Validate = function (word)
{
  this.rule = /^[a-m]$/;

  if (word)
  {
     return this.rule.test(word);
  }
  else
  {
     return {
        getRule   :   function()
           { return this.rule;}
            };
  }

}();

This works the first time if you call it with no arguments, but the second time I get the following error:

TypeError: object is not a function
  • 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-10T22:59:39+00:00Added an answer on June 10, 2026 at 10:59 pm

    As you are calling the function directly, the word parameter is always undefined, this is the global scope (window), so the code does the same as:

    var rule = /^[a-m]$/;
    var Validate = {
      getRule: function() { return this.rule; }
    };
    

    If you want something to work as both a function and an object, declare the function and then add properties to it (as a function is actually an object):

    var Validate = (function(){
    
      var rule = /^[a-m]$/;
    
      function validate(word) {
        return rule.test(word);
      }
    
      validate.getRule = function() { return rule; };
    
      return validate;
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom javascript class that inherits from google.maps.Map (V3 API).
I am trying to create a class that implements the IUnknown interface. I have
I'm trying to create a class that can instantiate arrays at runtime by giving
I am trying to create a simple function that can take a parameter and
Take the following piece of javascript that is trying to implement a class: function
I am using John Resig's Simple JavaScript Inheritance to create a class that can
I'm trying to create a simple extensible class in javascript but when setting property
I'm trying to create a class that inherits from ListBox and adding the property
I'm trying to create a class that manages the events of a combo box
I'm trying to create a function that will check to make sure all the

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.