-1

Can you explain me what's the difference between ModelState.IsValid and ModelState.IsValid() in the example below? (Both of them are working)

var func = function () {
  var ModelState = {
    IsValid: function () {
      return true
    }
  };
  
  // ModelState.Isvalid() is working, too
  document.body.innerHTML = ModelState.IsValid 
      ? 'ModelState is valid' : 'ModelState is invalid'
};
<button onclick="func()">Click me</button>

As you can see above, ModelState is a json object. If I can call ModelState.IsValid, is IsValid called json property in this case?

Tân
  • 1
  • 15
  • 56
  • 102
  • 2
    ModelState is not a JSON object. Its an object literal. And IsValid is a property of the ModelState object literal. – Tarun Dugar Jan 23 '16 at 07:35
  • 1
    @TarunDugar is correct. I think you are confused on the the difference between calling a function without parenthesis. Here is a similar question to help you http://stackoverflow.com/questions/3246928/in-javascript-does-it-make-a-difference-if-i-call-a-function-with-parentheses – John Roca Jan 23 '16 at 07:42

2 Answers2

1

If I can call ModelState.IsValid, is IsValid called json property in this case?

ModelState is an object literal and isValid is a property of this object.

if you change your code to

document.body.innerHTML = ModelState.Isvalid 
      ? 'ModelState is valid' : 'ModelState is invalid'

it will output ModelState is invalid

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

IsValid is a function. So when you do this ModelState.IsValid, this will return the function and not its value.

ModelState.IsValid() this will execute it and give you return value.

var func = function () {
  var ModelState = {
    IsValid: function () {
      return true
    }
  };
  
  console.log("ModelState.IsValid: ", ModelState.IsValid)
  
  console.log("ModelState.IsValid():", ModelState.IsValid())
  
  // ModelState.Isvalid() is working, too
  document.body.innerHTML = ModelState.IsValid 
      ? 'ModelState is valid' : 'ModelState is invalid'
};
<button onclick="func()">Click me</button>

Now, why ternary operator works:

JS tries to convert value to Boolean. If this is not possible, then availability is checked. So when you do

if("")

it will convert "" to 0 and 0 is considered as false, but if it has value, it is considered as true.

var a = "";

alert(a?true:false);

a = "function(){...}";

alert(a?true:false);
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thanks! I've tried and seen the difference. But when I change `document.body.innerHTML = ModelState.IsValid ? 'ModelState is valid' : 'ModelState is invalid'` to `document.body.innerHTML = ModelState.IsValid() ? 'ModelState is valid' : 'ModelState is invalid'`. I'm getting same result. – Tân Jan 23 '16 at 07:41
  • Can you tell me more about that? – Tân Jan 23 '16 at 07:42
  • I have updated my answer. Hope that helps – Rajesh Jan 23 '16 at 07:44
  • it looks like `implicit conversion 'string' to 'boolean'` – Tân Jan 23 '16 at 07:51
  • 1
    Yes. You can refer [following post](http://stackoverflow.com/questions/8692982/in-javascript-is-an-empty-string-always-false-as-a-boolean) for reference. – Rajesh Jan 23 '16 at 07:53
  • Many thanks! That's really help :) – Tân Jan 23 '16 at 07:57