Javascript & DHTML Instance Programming (Tutorials) Basics _ Basics

Source: Internet
Author: User
Tags assert exception handling
[2007-04-11 14:31:50 | Author:never-online]
First of all, please download jscript.chm this manual, no matter novice veteran, there is a manual is unavoidable, especially for beginners, if you do not Flip Rhino book, then this manual will be your first choice to understand the language. Most of the following, the manual can not be mentioned, or mention very little content.

The following tutorials are based on what you have said about the Jscript.chm manual, and if you haven't seen Jscript.chm, it's recommended that you download it, read the manual, and watch the tutorials.

The syntax of JS is almost the same as that of most Class C language, and the difference is only in its own characteristics. So the specific content of the grammar, I do not write more, read more about the manual should understand.

JS Five objects: String, Number, Boolean, Object, Function.

JS four kinds of loops:
for (var i=0; i<n; i++) {}
Do {} (TRUE);
while (true) {}
for (var i in collection) {}

Exception handling:
try {} catch (avariable) {}

JS syntax I do not enumerate, here only a few of the main JS to make some instructions, the manual may not be said.

A, string.
Strings are the most common. There are at least two ways to cast to a string:
1, with the string connector "+" number. + number in JS if the operation is added, if it is a string, is splicing, such as:
<script>
var a_number = 1000
var a_string = A_number + "";
</script>

2. Force transformation with string (string).
<script>
var a_number = 1000
var a_string = string (A_number);
</script>
Notice here that there is a forced transition, and there is no "new" keyword before the string. If you add the new keyword, you will get a string object. Objects can contain properties and methods, and strings cannot, the following can make a comparison:
<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)//will prompt JS
</script>
So, there's a difference between having new and not being new. This is true in Number,boolean, so there will be no more talk of such a transition in the future.

(number).
Here also talk about the issue of transformation.
In addition to being able to force a transition with number number, you can also use parseint, parsefloat to convert to integral or floating-point types. If the transition is not a number, then you will return Nan (not a), which can be judged by the isNaN function, where you can check the manual to see the grammar inside. By the way, remember this function.

Three, Boolean (Boolean).
This one is more troublesome, because JS in the processing of it is more peculiar.
In addition to what the JScript manual says: "
An expression that evaluates to True or FALSE. Non-Boolean expressions can also be converted to Boolean values, if required, but follow these rules:

All objects are treated as true.
When and only if the string is empty, the string is treated as false.
Null and undefined are treated as false.
When and only if the number is zero, the number is treated as false.
"Besides, you should also note:

First, before the cast is not coerced to a Boolean, which is not true or false
1, in the digital conditions of judgment, under normal circumstances is three kinds of situations: 0, negative, positive, as long as the right and wrong 0 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: The conditional judgment in the above example is directly judged by the conditional statement if we change the conditional statement to:
<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 can have very different results.

2, in the string, also need to note
<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: The conditional judgment in the above example is directly judged by the conditional statement if we change the conditional statement to:
<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 be very different results. Therefore, be careful when dealing with this problem.

Perhaps some friends see here will be a little dizzy, then how to like the manual said, only "", 0,null,undefined these ability is false? There are at least two ways of doing this:

(i), mandatory transformation:
1, with the above mentioned Boolean (AVar) to transform.
2, with "non-operator" to transform. Like the previous 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>
Two inverse, converts the AVar to a Boolean, equivalent to a Boolean (AVar).

(ii), congruent operators.
The strict equality operator is three equals "= =", which is different from what is said above, and it only makes comparisons of the same type. In the example above, it only compares true or false, and if it is false with a string or a number, it is true only when compared to 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>

Objects (object).
JS creates objects with at least two ways:

1, as mentioned above, with the new keyword. such as new number, new string ("string"), new Object (), New Customfunction (), and so on.
This method is described in detail in the manual, and there is no more to say here.

2, can also be enclosed in curly braces. Like what
var o = {
M1: ' Never-online.net ',
M2: ' blog '
}
This method is more time-saving and labor-saving. Using this method to create objects, it is necessary to note that the
Each member has a ":" Colon followed by a colon that is the contents of the member.
Secondly, there is a comma "," after the member content, but only the last member is not a comma.

Functions (function).
The function has two functions in JS,
One is called as a normal function.
Second, it can be used as a "class".
The first article has nothing to explain, the manual is very clear, the second article on a brief description.
In the 4th above, when it comes to objects, in addition to creating the object of JS itself, you need to create a class instance, then you must first write "class". This class is a function.
Like what:
<script>
function MyClass () {
This.m1= "MEMBER--M1";
This.m2= "member--m2";
}
var o = new MyClass ();
</script>

Vi. about this and new keywords.

Perhaps some friends are not quite sure what the purpose of this is. This is what the object-oriented approach refers to.

Here also briefly, this is the meaning of "own", and the above "oneself", refers to the MyClass.
For example, the MyClass class is a stencil with a name (M1) and a screw (m2), and the new keyword can be understood as "production." The above code can then be interpreted as:

(stencil MyClass) function MyClass () {

(the name of the mold MyClass is) this.m1= "MEMBER--M1"
(The screw on the mould MyClass is) this.m2= "member--m2";
}

Produce a product according to the style of the mold MyClass o
var o= new MyClass ();

This newly-released product has all the features of the mold MyClass. Of course, we can produce thousands of them according to the pattern of this mold.

If we want to, we can also modify his attributes, for example, I finished a product, want to change his name. We can do that, too.

var product = new MyClass ();
PRODUCT.M1 = "Newproduct"

The above explanation, hope to be clear some.

Basically put the basic knowledge to say some simple, JS basic knowledge in fact there are many, know that there is negligence, but also inconvenient to write more, write more cumbersome, only take a step to see what is not clear, can be written 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.