0

I am trying to convert this code to a namespace to make it cleaner and to avoid polluting the global namespace. I'm fairly new to this, and could use a little guidance or examples how I would convert the following code into a javascript namespace.

function Validator(fields) {
     this.fields = fields;
}

Validator.prototype.validate = function(form) {
  for(var i = 0, l=this.fields.length; i < l; i++) {
    alert(this.fields[i].value);
        if (this.fields[i].value == 0) {
            alert("The field  is empty");
            return false;
        }
    }
}

var validator = new Validator([ "username", "password"]);

function runValidate(form) {
validator.validate(form);
    }

(I know this OO approach to validation is excessive!) I call this runValidate from a button in a form like this "runValidate(this.form)".

3
  • Is that runValidate function really necessary? It saves you only 7 chars. Commented Feb 27, 2013 at 10:14
  • Thanks. what should I do instead of it? Commented Feb 27, 2013 at 13:29
  • Just directly call validator.validate(this.form) from the button Commented Feb 27, 2013 at 13:43

2 Answers 2

2

Namespaces are just javascript objects, for example

var myNamespace = {};
myNamespace.Validator = function(fields) {
   ...
}
myNamespace.Validator.prototype.validate = function(form) {
   ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. Should I do the same with my runValidate function? how would I set var validator?
would var validator = new Validator([ "username", "password"]); become var validator = new myNamespace.Validator([ "username", "password"]); ?
Correct, you now call new myNamespace.Validator instead of new Validator. And then use validator.validate, as @Bergi suggested, I see no point in having runValidate function.
1

Javascript doesn't have native namespaces but you could use plain and simple objects to simulate that. Here's a simple implementation of a namespace utility function:

function namespace(namespaceString) {
  var nodes = namespaceString.split('.'),
      parent = window,
      currentNode;    

  for(var i = 0, length = nodes.length; i < length; i++) {
    currentNode = nodes[i];
    parent[currentNode] = parent[currentNode] || {};
    parent = parent[currentNode];
  }

  return parent;
}

You can use like this:

var MyApp = namespace("MyApp");
MyApp.Validator = function(fields) {
  this.fields = fields;
}

var validator = new MyApp.Validator(["username", "password"]);
// this also works:
var validator = new namespace("MyApp.Validator")(["username", "password"]);

This will keep you from poluting your global namespace but you will still have some global variables: MyApp in this case and any other root node in your namespaces.

2 Comments

Many thanks. Would the function call from the button in the form remain the same?
Yes. Even if it's not the best way to go, it should work just fine.

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.