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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:07:33+00:00 2026-06-03T08:07:33+00:00

I’m building a small Validate object which basically exposes a validate method and takes

  • 0

I’m building a small “Validate” object which basically exposes a validate method and takes the id of one element and an array of validators and then returns true or false.

basically this is what i’d like to achieve

var Validator = function() {
  var no_digits = function( el ) {
      return true;
  }
  var no_uppercase_letters = function( el ) {
      return true;
  }
  return {
     validate: function( element_id, validators ) {

        //here i would like to iterate on the validators array and for each 
        //element of the array i would like to check if a function of the same name 
        // exist and call that function passing the element      

     }
  }
}();

and then call it like this

var element_valid = Validator.validate( 'myid', [ "no_digits", "no_uppercase_letters"] );

where the second parameter is an array of validators i’d like to call.

Any suggestion on a good object oriented approach?I would like to keep the validating functions private otherwise i could do

var Validator = function() {


    return {
        validate: function(element_id, validators) {
            console.log(this);
            this[validators]();

            // Validator[validators](element_id);     
        },
        no_digits: function(el) {
            alert('hi');
            return true;
        },
        no_uppercase_letters:  function(el) {
            return true;
        }
    }
}();

but i’d rather keep the no_gits an no_uppercase_letters function private

var element_valid = Validator.validate(‘myid’, “no_digits”);

  • 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-03T08:07:34+00:00Added an answer on June 3, 2026 at 8:07 am
    var valdiate = (function() {  
    
         var _p={};//toss all private members into a single object.
         _p.list=[];
         _p.init=function(){
           _p.list=[];
         };
    
        var noDigits = function() {
    
        };
    
        //public members. use "this" to reference object followed by the method.
        //however valdiate._p.list won't be accessible to the global scope    
        return {    
            check: function() {
                  noDigits();
            },
            fnNaMe1:function(){
                  _p.init();
            },
            fnName2:function(){
               return _p.list.slice(0);//return a clone  
            }
        };
    })();
    

    It’s called the “Module pattern”. This pattern is more commonly known as simply “encapsulation” in JavaScript. Closures is another possibly but more specifically it is purely encapsulation in this case.

    Encapsulation simply means you are making some members private. What is private in this case? Well the noDigits variable is private in this case.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.