Js Object-Oriented Programming: Which of the following conditions can be used in if statements?

Source: Internet
Author: User

Js Object-Oriented Programming: Which of the following conditions can be used in if statements?

In all programming languages, if is one of the longest-used judgments. But in Javascript, what can be used as a judgment Expression in if?

For example, if there are just a few lines, but a few parentheses are missing, the true and false are completely different. What exactly does it 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. variables defined in the first category are considered false in if, but are not assigned a value.

For example:

Var t; if (t) {alert ("true defined not assigned value");} else {alert ("false defined not assigned value ");}

2. For the second type of Defined variables, the value assigned to a null string is considered false in if, and the value assigned to another string, that is, it is considered true if the string contains characters.

For example:

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

If is false

For 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 is true, that is, if the string type has any character, it is true even if it is a space character.

3. For the third type of Defined variables, if the value is true and if the value is false, the value is false, which is the same as the type of bool variables in other languages.

For example:

          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. For the fourth type of Defined variables, the value 0 is false in if, and other values are considered true, which is the same as the type of values in C.

For example:

          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 are false.

          var  t;            t=2;   if(t)   {     alert("true  t=2;");   }   else   {     alert("false  t=2;");   }

We found that non-0 values are true.

5. The special values null and undefined in the fifth type of js 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;");   }
Since the default value of undefined variables in js is undefined, this explains the first case.



6. Class 6 defined functions are divided into two types based on the call method.

First: No parentheses. if the definition is true, an error is returned if it is not defined.

function testfunction(){}   if(testfunction)   {     alert("true  testfunction;");   }   else   {     alert("false  testfunction;");   }
The second type: parentheses are actually equivalent to calling a function. Naturally, the true and false values are determined based on the return value of the function.

For example:

 function testfunction(){}  if(testfunction())   {     alert("true  testfunction;");   }   else   {     alert("false  testfunction;");   }
It is false because the return value is undefined if the function does not define the return value.



7. Class 7 defined objects. if they are not assigned a value, they are false and true after being assigned a value.

For example:

var obj;    if(obj)   {     alert("true  obj;");   }   else   {     alert("false  obj;");   }
In fact, in js, variables have no type when no value is assigned, so they are the same as in the first case.

However, after a value assignment, it will become true, for example:

          var <span style="font-family: Arial, Helvetica, sans-serif;">obj</span>;            obj={};    if(obj)   {     alert("true  obj={};");   }   else   {     alert("false  obj={};");   }

8. The attribute fields of the eighth type defined objects are the same as those of individual variables. For example, if the numeric value is 0, the attribute fields are false. If the other values are true and the string value is null, the attribute fields are false, others are true.

For example

 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 ninth defined object is the same as that of a single function,

If no brackets are added, it is false,

Var obj = {}; obj. funtext = function () {}; if (obj. funtext) {alert ("true obj. funtext; ");} else {alert (" false obj. funtext ");} if (obj. funtext1) // the property is not defined, and no method is defined {alert ("true obj. funtext1; ");} else {alert (" false obj. funtext1 ");}


Adding parentheses is equivalent to calling a method, that is, determining whether the result is true or false Based on the returned 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 can be seen that there are many types of judgment in js, but they can all be seen as deformation of these types. As long as you have mastered these basic features, you can use the if Statements flexibly.

The most basic types are null, undefined, and if. For the value type, 0 is false, and other values are true. For the character type, null strings are false, others are true, and for the method attribute, if the definition is true, otherwise it is false. Other applications can be seen as disguised applications.


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.