0

I want to create a method to validate data like C# syntax by using javascript. My C# code is like this:

public static class Validate
{
     public static bool IsValid(this string modelState)
     {
          return !String.IsNullOrEmpty(modelState) ? true : false;
     }
}

bool Method()
{
     string modelState = "My model";
     return modelState.IsValid();
}

In javascript, I want to convert the code to:

var IsValid = function (modelState) {
     return $.trim(modelState).length > 0 ? true : false;
}

var method = function () {
     var modelState = "My model";
     // how to:
     // return modelState.IsValid();
}

I had referenced this question, but it doesn't solve my problem.

One of the answers said:

function Foo() {};
Foo.talk = function() { alert('I am talking.'); };
Foo.talk();

A Foo is an object, but in my example, a modelState is a string.

Is there a way to do that in javascript?

Community
  • 1
  • 1
  • 1
    What you are talking about in C# is not JUST a static method but an extension method. Yes this possible in JavaScript. Have a look at other SO post I found. http://stackoverflow.com/questions/9354298/how-do-i-write-an-extension-method-in-javascript – Nikhil Vartak Sep 29 '15 at 09:57
  • @vnikhil Thanks for your searching. –  Sep 29 '15 at 10:09

3 Answers3

2

To extend an object type in javascript, you can extend its prototype. Discussions can arise whether you should extend base types, but it could be done as follows:

String.prototype.IsValid = function () {
     return $.trim(this).length > 0 ? true : false;
}


var method = function () {
     var modelState = "My model";
     // how to:
     return modelState.IsValid();
}
Me.Name
  • 12,259
  • 3
  • 31
  • 48
0

Your C# method is conceptually not a static method, even though it literally is one. It's an extension method. That means that it shows up on all instances of strings (in terms of what you see when editing your source code), as though it were an instance method.

You can do extension methods in JavaScript as well, because of the prototypical nature of the language: You add them to the prototype, in this case, String.prototype.

In general, if you're going to extend the prototype of a built-in object like String, it's best to use Object.defineProperty (ES5+) so that the property you create is non-enumerable:

Object.defineProperty(String.prototype, "isValid", {
    value: function() {
        // Refer to the string using `this`
    }
});

That's preferred over just doing it with assignment:

String.prototype.isValid = function() {
    // Refer to the string using `this`
};

...since the latter creates an enumerable property.

If your code is going to intermix with other code (for instance, if it's part of a library), beware of extending built-in prototypes even using Object.defineProperty because of the possibility of name collision (e.g., if your library adds isValid and someone else's code also adds isValid).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you. Can you explain to me a sub-question? In C#, `IsValid()` method cannot be overridden, how about in javascript? –  Sep 29 '15 at 10:06
  • @Kevin: It could be in JavaScript, yes. The other difference is that it actually is a method on strings at runtime in JavaScript, whereas a C# extension method is purely a source code illusion; the actual compiled result is a static method call. – T.J. Crowder Sep 29 '15 at 10:10
0

You could extend the String.prototype by adding IsValid method, or use Object.defineProperty to only extend you local modelState:

var IsValid = function () {
     return this.trim().length > 0;  // removed jQuery dependence, IE9+
}

var method = function () {
     var modelState = new String("My model");
    Object.defineProperty( modelState, "IsValid", {
      get: function() { return IsValid; },
      enumerable: false,
      configurable: false
    });
    return modelState.IsValid()
}

http://jsfiddle.net/xs2trt4x/1/

pawel
  • 35,827
  • 7
  • 56
  • 53