JS Object-Oriented Programming: What can be used as an inference condition in the IF?

Source: Internet
Author: User

in all programming languages if is one of the longest-used inferences. But in JS, what can be inferred expressions in the IF Chinese?

For example, how many lines, just one less parenthesis. True or false is totally different. What does that mean?

        var obj={};         Obj. Funtext=function () {};    if (obj. Funtext)   {     alert ("True  obj. Funtext; ");   }   else   {     alert ("False  obj. Funtext ");   }   Obj. Funtext=function () {};    if (obj. Funtext ())   {     alert ("True  obj. Funtext (); ");   else   {     alert ("False  obj. Funtext () ");   }
1 The first class of defined variables but not assigned values in if are false

Like what:

var  t;       if (t)   {     alert ("True defined Unassigned");   }   else   {     alert ("False defined Unassigned");   }

2 The second class of defined variables, the assignment of an empty string to the if is thought to be false. Assigns a string to a different value. That is, there are characters in the string that you think are true.

Like what:

        var  t;          T= "";   if (t)   {     alert ("True t= ';");   }   else   {     alert ("false t=");   }

If inference is false

Another example:

var  t;           T= "";   if (t)   {     alert ("True t= ';");   }   else   {     alert ("false t=");   }    T= "111";   if (t)   {     alert ("True t= ' 111 ';");   }   else   {     alert ("False t= ' 111 '");   }
If inference is true, that is, for string types. Just have characters, even if the space character is inferred is true.

3 The third class of defined variables. An assignment of true is true in if, and the assignment is false, which is the same as a variable of type bool in other languages.

Like what:

          var  t;           T=false;   if (t)   {     alert ("True  t=false;");   }   else   {     alert ("False  t=false;");   }    T=true;   if (t)   {     alert ("True  t=true;");   }   else   {     alert ("False  t=true;");   }

4 The Fourth class has a defined variable, an assignment of 0 is false in the IF, and the other values feel true, which is the same as the variable of the type of the numeric value in the C language.

Like what:

          var  t;           t=0;   if (t)   {     alert ("True  t=0;");   }   else   {     alert ("False  t=0;");   }    t=0.0;   if (t)   {     alert ("True  t=0.0;");   }   else   {     alert ("False  t=0.0;");   }
The test found that both 0 and 0.0 were false.

          var  t;            t=2;   if (t)   {     alert ("True  t=2;");   }   else   {     alert ("False  t=2;");   }

found that non-0 is true.

5 The special value of the fifth class JS null,undefined, are false

var  t=null;   if (t)   {     alert ("True  t=null;");   }   else   {     alert ("False  t=null;");   }    t=undefined;   if (t)   {     alert ("True  t=undefined;");   }   else   {     alert ("False  t=undefined;");   }
Because the default value of a variable that is not defined in JS is undefined. So that explains the first kind of situation.



6 The Sixth class has defined functions. There are two kinds of methods according to the calling method

The first type: without parentheses, assuming that the definition is true, the undefined will be an error

function TestFunction () {}   if (testfunction)   {     alert ("True  testfunction;");   }   else   {     alert ("False  testfunction;");   }
Another type of: Parenthesized. In fact, the equivalent of calling a function, which is naturally inferred from the function's return value

Like what:

function TestFunction () {}  if (TestFunction ())   {     alert ("True  testfunction;");   }   else   {     alert ("False  testfunction;");   }
is false, because the function assumes that the return value value is undefined. The return value is undefined



7 The seventh class of defined objects. If it is not assigned, it is false in the IF, and is true after the value is assigned.


Like what:

var obj;    if (obj)   {     alert ("True  obj;");   }   else   {     alert ("False  obj;");   }
In fact, since the variables in JS are not typed when they are not assigned, they are the same as in the first case.

But after the assignment, it becomes true, such as:

          var <span style= "Font-family:arial, Helvetica, Sans-serif;" >obj</span>;            obj={};    if (obj)   {     alert ("True  obj={};");   }   else   {     alert ("False  obj={};");   }

8 The property field for the eighth class of defined objects is the same as a separate variable, such as a value of 0 o'clock is false. The other is true, the string type is null when False, the other is true.

Like what

var obj={};   Obj. Text= "";    if (obj. Text)   {     alert ("True  obj. Text; ");   }   else   {     alert ("False  obj. Text ");   }    Obj. text= "Text";    if (obj. Text)   {     alert ("True  obj. Text; ");   }   else   {     alert ("False  obj. Text ");   }   Obj. text=0;    if (obj. Text)   {     alert ("True  obj. Text; ");   }   else   {     alert ("False  obj. Text ");   }    Obj. Text=1;    if (obj. Text)   {     alert ("True  obj. Text; ");   }   else   {     alert ("False  obj. Text ");   }

9 The method of the Nineth class of defined objects. Is the same as a separate function,

No parentheses are assumed to be undefined or false.

var obj={};    Obj. Funtext=function () {};    if (obj. Funtext)   {     alert ("True  obj. Funtext; ");   }   else   {     alert ("False  obj. Funtext ");   }      if (obj. FUNTEXT1)//undefined attribute, nor method   {     alert ("True obj") is defined  . Funtext1; ");   }   else   {     alert ("False  obj. Funtext1 ");   }


Parentheses are equivalent to calling methods, which are inferred from the return value.

var obj={};     Obj. Funtext=function () {};    if (obj. Funtext ())   {     alert ("True  obj. Funtext (); ");   else   {     alert ("False  obj. Funtext () ");   }    Obj. Funtext2=function () {return "FF"};    if (obj. FUNTEXT2 ())   {     alert ("True  obj. Funtext2 (); ");   else   {     alert ("False  obj. Funtext2 () ");   }


It is possible to see that there are many types in JS that can be inferred in if, but can finally be seen as variants of these types. Only by mastering the most important of these, you can use the if inference flexibly.

The most basic is null. Undefined,if inference is false, for numeric types. 0 is False, the other is true; for a character type an empty string is false, the other is true, for the method property. The assumption is that the definition is true. Otherwise it is false, and all else can be seen as a variation of these applications.


JS Object-Oriented Programming: What can be used as an inference condition in the IF?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.