Javascript & DHTML instance Programming

Source: Internet
Author: User
[14:31:50 | Author: Never-Online]
Please download JScript for this section. the CHM manual, no matter the novice or veteran, is inevitable. Especially for beginners, if you do not have an empty rhino book, this manual will be your first choice for understanding the language. The majority of the content mentioned below can be omitted in the manual or a few.
The following tutorials are for the JScript you mentioned above. based on the knowledge of CHM, if you have not read JScript. CHM. We recommend that you download it first, read the manual, and read the tutorial.
The syntax of JS is similar to that of most C-like languages. The difference lies only in its own features. So I will not write more details about the syntax. I should read more about the manual.
Five major JS objects: String, number, Boolean, object, and function.
JS four loops:
For (VAR I = 0; I <n; I ++ ){}
Do {} while (true );
While (true ){}
For (var I in Collection ){}
Exception Handling:
Try {} catch (avariable ){}
I will not list the JS syntax one by one. Here, I will explain only a few JS objects, but I may not have mentioned this in the manual.
1. String.
Strings are the most commonly used. There are at least two ways to forcibly convert a string:
1. Use the string connector "+. + In JS, if it is an operation, it is a plus. If it is a string, it is concatenated. For example:
<SCRIPT>
VaR a_number = 1000
VaR a_string = a_number + "";
</SCRIPT>
2. Use string to force transformation (string ).
<SCRIPT>
VaR a_number = 1000
VaR a_string = string (a_number );
</SCRIPT>
Note that the above is about forced transformation, and there is no "new" keyword before the string. If the new keyword is added, a string object is obtained. Objects can contain attributes and methods, but strings cannot. You can make a comparison as follows:
<SCRIPT>
VaR a_number = 1000
VaR a_string = string (a_number );
A_string.property = "JS ";
Alert (a_string.property) // will prompt undefined
VaR a_object = new string (a_number)
A_object.property = "JS ";
Alert (a_object.property) // JS will be prompted
</SCRIPT>
Therefore, there is a difference between new and new. This is true in number and Boolean, so we will not talk about this transformation in the future.
2. Number ).
Here we also talk about the transformation issue.
Division
You can use number to force the transformation, or use parseint and parsefloat to convert it into an integer or floating point type. If it is not a number after the transformation, Nan will be returned
(Not a number). You can use the isnan function to determine the number. Here you can check the manual to see the syntax. Remember this function by the way.
3. boolean ).
This one is a little more troublesome, because JS processes it strangely.
In addition to the JScript manual,
An expression with a value of true or false. If needed, non-boolean expressions can also be converted to boolean values, but follow the following rules:
All objects are treated as true.
If and only if the string is null, the string is treated as false.
Null and undefined are treated as false.
If and only when the number is zero, the number is treated as false.
Note:
First, when it is neither true nor false before it is forcibly converted to a Boolean Type
1. In the numerical condition judgment, there are generally three situations: 0, negative, positive, as long as the value is not 0, it is true. The following is an example.
<SCRIPT>
VaR a = 0;
VaR B =-1;
VaR c = 1;
Function assert (Avar ){
If (Avar) Alert (true );
Else alert (false );
}
Assert (a) // false
Assert (B) // true
Assert (c) // true
</SCRIPT>
Note: In the preceding example, The Condition Statement is directly judged. If we change the condition statement:
<SCRIPT>
VaR a = 0;
VaR B =-1;
VaR c = 1;
Function assert (Avar ){
If (Avar = true) Alert (true );
Else alert (false );
}
Assert (a) // false
Assert (B) // false
Assert (c) // true
</SCRIPT>
Negative numbers have different results.
2. Note that
<SCRIPT>
Function assert (Avar ){
If (Avar) Alert (true );
Else alert (false );
}
VaR A = "undefined ";
VaR B = "false ";
VaR c = "";
Assert (a) // true
Assert (B) // true
Assert (c) // false
</SCRIPT>
Note: In the preceding example, The Condition Statement is directly judged. If we change the condition statement:
<SCRIPT>
Function assert (Avar ){
If (Avar = true) Alert (true );
Else alert (false );
}
VaR A = "undefined ";
VaR B = "false ";
VaR c = "";
Assert (a) // false
Assert (B) // false
Assert (c) // false
</SCRIPT>
There will also be very different results. Therefore, be careful when dealing with such problems.
Some friends may be a little dizzy when they see it. How can they be false only when "", 0, null, and undefined are described in the manual? There are at least two methods:
(1) Forced Transformation:
1. Use the previously mentioned Boolean (Avar) for transformation.
2. Use non-operators for transformation. For example
<SCRIPT>
Function assert (Avar ){
If (!! Avar = true) Alert (true );
Else alert (false );
}
VaR A = "undefined ";
VaR B = "false ";
VaR c = "";
Assert (a) // true
Assert (B) // true
Assert (c) // false
</SCRIPT>
Convert Avar to boolean type, which is equivalent to boolean (Avar ).
(2), full and other operators.
The equal all operator is three equal to "=", which is different from the above mentioned. It only performs comparison of the same type. As mentioned in the above example, it only compares true or false. If it is equal to a string or number, it is false. It is true only when compared with true. Example:
<SCRIPT>
Function assert (Avar ){
If (Avar = true) Alert (true );
Else alert (false );
}
VaR A = "undefined ";
VaR B = true;
VaR c = 1;
Assert (a) // false
Assert (B) // true
Assert (c) // false
</SCRIPT>
4. object ).
There are at least two methods to create an object in JS:
1. As mentioned above, use the new keyword. For example, new number (100), new string ("string"), new object (), and new customfunction.
This method is described in detail in the manual.
2. Brackets can also be used. For example
VaR o = {
M1: 'Never -online.net ',
M2: 'blog'
}
This method saves time and effort. To create an object using this method, note that,
Each member has a colon (:) followed by a colon.
Second, there is a comma "," after the member content, but only the last member has no comma.
5. functions ).
Functions have two functions in Js,
First, it is called as a common function.
Second, it can be used as a "class.
There is nothing to explain in the first article. The manual is very clear, and the second article is a brief description.
As mentioned in the fourth point above, in addition to creating JS objects, you need to create a class instance, so you must first write the "class. This class is a function.
For example:
<SCRIPT>
Function myclass (){
This. M1 = "member -- M1 ";
This. m2 = "member -- m2 ";
}
VaR o = new myclass ();
</SCRIPT>
6. Keyword about this and new.
Some friends may not know what the role of this is. This is the content mentioned in Object-Oriented
Here, we also briefly describe that this means "yourself", and the above "yourself" refers to myclass.
For example, the myclass class is a mold with a name (M1) and a screw (m2) on the mold, and the New Keyword can be understood as "production ". Then you can Code As follows:
(Mold myclass) function myclass (){
(The name of the mold myclass is) This. M1 = "member -- M1"
(The screw above the mold myclass is) This. m2 = "member -- m2 ";
}
Produce a product O according to the mold myclass Style
VaR o = new myclass ();
This newly released product has all the features of the mold myclass. Of course, we can produce thousands of pieces according to the mold style.
If we want to, we can modify his attributes. For example, after I have produced a product, I want to change his name. We can do the same.
VaR Product = new myclass ();
Product. M1 = "newproduct"
I hope to be clear about the above.
I have simply explained the basic knowledge I want to talk about. In fact, there are also a lot of basic JS knowledge. I know that there is negligence, but it is inconvenient to write more. It is cumbersome to write more, only by taking a step and looking at what else is unclear can we write it again
Related Article

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.