Home  >  Article  >  Web Front-end  >  Javascript & DHTML Example Programming (Tutorial) Basics_Basic Knowledge

Javascript & DHTML Example Programming (Tutorial) Basics_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 19:12:491080browse

[ 2007-04-11 14:31:50 | Author: never-online ]
First of all, please download the JScript.chm manual. No matter whether you are a novice or an expert, a manual is inevitable, especially for novices. If you haven't flipped through the Rhino book, then this manual will be your first choice for understanding the language. Most of the things mentioned below may not be mentioned in the manual, or may be mentioned very little.

The following tutorials are written based on your understanding of the JScript.chm manual mentioned above. If you have not read JScript.chm, it is recommended that you download it first and read the manual. , while watching the tutorial.

The syntax of JS is similar to that of most C-like languages. The difference is only in its own characteristics. Therefore, I will not write more about the specific content of the grammar. You should understand it by reading the manual.

The five major objects of JS: String, Number, Boolean, Object, Function.

Four types of JS loops:
for(var i=0; ido {} while(true);
while(true) {}
for (var i in collection) {}

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

I won’t list all the JS syntax here. Some explanations of several major objects of JS may not be mentioned in the manual.

1. String.
Strings are the most commonly used. There are at least two ways to force conversion into a string:
1. Use the string connector " ". In JS, if the number is an operation, it is addition, if it is a string, it is concatenation, for example:



2. Use String to force conversion (String).


One thing to note here is that the above is forced transformation, and there is no "new" keyword before String. If you add the new keyword, you will get a String object. Objects can contain properties and methods, but strings cannot. A comparison can be made as follows:


So, there is a difference between new and no new. This is true for Number and Boolean, so I won’t say more about this transformation in the future.

2. Number.
Here we also talk about the issue of transformation.
In addition to using Number to force conversion, you can also use parseInt and parseFloat to convert to integer or floating point type. If it is not a number after conversion, then NaN (Not a Number) will be returned. At this time, you can use the isNaN function to judge. Here you can check the manual to see the syntax inside. By the way, remember this function.

3. Boolean.
This one is a bit more troublesome, because the processing of it in JS is rather strange.
In addition to what the JScript manual says: "
An expression whose value is true or false . Non-Boolean expressions can also be converted to Boolean values ​​if necessary, but the following rules must be followed:

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

First of all, before it is cast to a Boolean type, that is, when it is not true or false
1. In digital conditional judgment, there are generally three situations: 0, negative number, positive number. As long as it is non-0, it is true. The following is an example.


Note: The conditional judgment in the above example directly judges the conditional statement. If we change the conditional statement to:


Negative numbers will have completely different results.

2. In strings, you also need to pay attention to



Note: The conditional judgment in the above example directly judges the conditional statement. If we change the conditional statement to :


will also have completely different results. Therefore, be careful when dealing with this aspect.

Maybe some friends will be a little dizzy when they see this, so how can we make it as stated in the manual that only "", 0, null, and undefined can be false? There are at least two methods:

(1) Forced transformation:
1. Use the Boolean (aVar) mentioned above to transform.
2. Use the "not operator" to transform. For example, in the above example


two negations are used to convert aVar into a Boolean type, which is equivalent to Boolean(aVar).

(2), congruent operator.
The identity operator is three equals "===", which is different from what is said above. It only performs comparisons of the same type. As mentioned in the above example, it only compares true or false. If compared with strings or numbers, both are false. Only when compared with true, is it true. Example:


<script> <BR>var a_number = 1000 <BR>var a_string = a_number + ""; <BR></script> 4. Object. <script> <BR>var a_number = 1000 <BR>var a_string = String(a_number); <BR></script>JS has at least the following two methods to create objects: <script> <BR>var a_number = 1000 <BR>var a_string = String(a_number); <BR>a_string.property = "js"; <BR>alert(a_string.property) //将提示undefined <br><br>var a_object = new String(a_number) <BR>a_object.property = "js"; <BR>alert(a_object.property) //将提示js <BR></script><script> <BR>var a = 0; <BR>var b = -1; <BR>var c = 1; <br><br>function assert (aVar) { <BR>if (aVar) alert(true); <BR>else alert(false); <BR>} <BR>assert(a) // false <BR>assert(b) // true <BR>assert(c) // true <BR></script>1. As mentioned above, use the new keyword. For example, new Number(100), new String("string"), new Object(), new customFunction(), etc. <script> <BR>var a = 0; <BR>var b = -1; <BR>var c = 1; <br><br>function assert (aVar) { <BR>if (aVar==true) alert(true); <BR>else alert(false); <BR>} <BR>assert(a) // false <BR>assert(b) // false <BR>assert(c) // true <BR></script>This method is explained in detail in the manual, so I won’t go into details here. <script> <BR>function assert (aVar) { <BR>if (aVar) alert(true); <BR>else alert(false); <BR>} <br><br>var a="undefined"; <BR>var b="false"; <BR>var c=""; <br><br>assert(a) // true <BR>assert(b) // true <BR>assert(c) // false <BR></script><script> <BR>function assert (aVar) { <BR>if (aVar==true) alert(true); <BR>else alert(false); <BR>} <br><br>var a="undefined"; <BR>var b="false"; <BR>var c=""; <br><br>assert(a) // false <BR>assert(b) // false <BR>assert(c) // false <BR></script>2. You can also use curly brackets.比如
var o = { 
  m1:'never-online.net',
  m2:'blog' 
}
这种方法就比较省时省力了。利用这种方法来创建对象,需要注意的就是,
每个成员后有一个":"冒号,冒号后是该成员的内容。
其次就是,成员内容后有一个逗号",",但仅最后一个成员是没有逗号的。

五、函数(Function)。
函数在JS里的作用有两个,
一是做为一个普通函数一样被调用。
二是可以做为一个"类"(class)来使用。
第一条就没有什么可说明的了,手册上说得很清楚了,第二条就简要说明一下。
上面第四点里说到对象,除了创建JS本身的对象之外,需要创建一个类的实例,那么就必须先把“类”写出来。这个类就是Function。
比如:
<script> <BR>function myclass() { <BR> this.m1="member--m1"; <BR> this.m2="member--m2"; <BR>} <BR>var o = new myclass(); <BR></script>

六、关于this和new关键字。

也许有些朋友还不太清楚这个this的作用是什么。这是面向对象里所提及的内容

这里也简单说一下,this就是“自己”的意思,而上面的的“自己”,就是指myclass。
举个例子来说myclass这个类就是一个模具,模具上有一个名字(m1),还有一个螺丝(m2),而new关键字就可以理解成“生产”。那么就可以把上面的代码理解成:

(模具 myclass)function myclass() {

(模具myclass的名字是)this.m1="member--m1"
(模具myclass上面的螺丝是)this.m2="member--m2";
}

按照模具myclass的样式生产一个产品o
var o= new myclass();

这个刚出炉的产品就有模具myclass的所有特性了。当然,我们可以按照这个模具的样式生产成千上万个。

如果我们愿意,我们还可以修改一下他的属性,比如,我生产完一个产品,想把他的名字换了。我们也可以这么做

var product = new myclass();
product.m1 = "newProduct"

上面这样讲解,希望能清楚一些。

基本把要说的基础知识简单的说了一些,JS的基础知识其实也有很多,知道有疏忽,但是又不便多写,写多了就烦琐了,只有走一步看一步了,看看还有什么不清楚的,才能再写出来了 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn