Home > Web Front-end > JS Tutorial > body text

Introduction to Javascript, the first article js basics_basic knowledge

PHP中文网
Release: 2016-05-16 19:03:05
Original
1150 people have browsed it

These are the notes I took after reading The Art of DOM Programming, Understanding JavaScript, and The Authoritative Guide to JavaScript 5. I am not qualified enough to write advanced articles. If you think the notes are not well written, you don’t have to read my future ones. This article is just a waste of a few minutes of your time.

Javascript learning first article js basics

1, javascript character set:
javascript uses the Unicode character set encoding.
Why use this encoding?
The reason is simple, 16-bit Unicode encoding can represent any written language of people on earth. This is an important feature of language internationalization. (You may have seen scripts written in Chinese, such as: function My function () {} );
Each character in Javascript is represented by 2 bytes. (Because it is 16-bit encoding)


2, case sensitive:
js is a case-sensitive language.
Note: I have made mistakes before.
HTML is not case sensitive. I often see people write like this,
(This is correct)
If you put it in JS, you must use onclick (lowercase) ! )
At the same time, only lowercase letters can be used in XHTML.
We don’t need to worry too much about this. For problems like this, you can actually set a standard for yourself and write all lowercase letters when writing your own programs.
In addition, the same goes for the semicolon after each line of the program, we write them all.

3, comments:
Single line:
// Comment 1
/* Comment 2 */
Multiple lines:
/* Comment 3
* Comment 3
* Note 3
*/

4 , Identifier:
Identifier is a name, used to name variables and functions.
Rules: The first letter must be a letter, an underscore (_), or a dollar sign ($).
Why can’t the first letter be a number?
If the first one is a number, js can easily treat it as a number, so the naming is meaningless. After js stipulates it, it is easy to distinguish between identifiers and numbers.

5, direct quantity:
is the data value directly displayed in the program.
For example: 12 , 1.2 , “ hello “ , true , null , [1,2,3,4]
These are all direct quantities.

6, reserved words and keywords:
For specific ones, you can go to google.cn.
Actually, as long as we don’t choose some very depressing names, there won’t be any conflict.

7, js data types:
3 basic types; numbers, strings and Boolean values.
2 small data types: null and undefined . (Why are they called small data types? Because they only define one value)
1 composite type: object. (In this type, its value can be basic Data type can also be a composite type, such as other objects. )
Note: There is a special object in the object----function. (It is an object that can execute code.)

Some other objects:
Array:
Date class: It is an object of date.
RegExp class: Regular expression object.
Error class: an object in which errors occur in js.

8 , things to pay attention to when using data types:
1): Numbers:
Because numbers include octal, decimal, hexadecimal, etc. . .
Octal: var num = 011; //Starts with "0"
Hexadecimal: var num =0x1f; //Starts with "0x"
So it is a language that can be recognized by js That said, you have to pay attention.
alert(377); // 377
alert(0377); //255 = 3 * 64 7 * 8 7 * 1

There is an important object for arithmetic operations: Math.
For details, you can download the manual online and check the methods inside.

2 useful functions: isNaN() and isFinite()

isNaN() : 用于检查其参数是否是非数字值。// 提示:是非数字哦。(not a number) 
document.write(isNaN(0) )  //返回false 
document.write(isNaN(5-2) ) //返回false 
document.write(isNaN ("Hello") ) //返回true
Copy after login

isFinite(number) function is used to check whether its argument is infinity.
If number is finite, return true. If number is NaN (not a number) or infinity, return false;

2): Characters:
'you're right';
If written like this, js will mistakenly think that it ends after the letter you, causing an error.
So when encountering this situation, escaping must be used.
We can write like this:
‘you're right';
Also: you can search google.com for escape sequence list.

Simple operation example of string:

var a = "cssrain"; 
var b = a.charAt(a.length-1); // 从字符串a中截取最后一个字符。  输出:n 
var c = a.substring(0 , 2);  // 从字符串a中截取第1,2个字符。 输出:cs 
var d = a.indexOf('s');   // 从字符串a中查找第一个s出现的位置。 输出:1
Copy after login


As you can see from the example, the bases all start from 0.

var e = a.substring( a.length-1 ); //可以看出,substring第2个参数不写的话, 
//默认 是到最后。 
var f = a.substring( a.length-1 , a.length);//等价于
Copy after login



3): Conversion between numbers and characters:
Numbers to characters:

var number_to_string = number + “ ”; //方法1:添加一个空的字符串。 
var number_to_string =String(number); //方法2:使用String()函数。 
var number_to_string =number. toString(); //方法3:使用toString()函数。
Copy after login


Note: The toString() method defaults to decimal conversion.
If you want to use octal conversion, you can write it like this: number. toString(8);

Convert characters to numbers:

var string_to_number = string – 0 ; //方法1: 字符串减去0。 
var string_to_number = Number(string) ; //方法2:使用Number ()函数。 
var string_to_number = parseInt(string) ; //方法3:使用parseInt ()函数。
Copy after login


方法1中不能 用string+0 ; 这样会导致字符串拼接,而不是类型转换。
方法2 中的Number函数转换,比较严格。
比如:

var a = "19cssrain86"; 
var b = Number(a);  //输出NaN.
Copy after login

如果我们使用方法3。

var c = parseInt(a); //输出 19
Copy after login


可以看出parseInt()会自动忽略非数字的部分。
parseInt()只取整数部分,忽略小数部分。
parseFloat()会把小数部分也取到。
和toString()一样,parseInt也有进制,默认是10进制。
如果想使用8进制,可以这么写: parseInt( “077” , 8 ); // 输出63 = 7 * 8 + 7
当字符以0开头的时候,我们必须把 第二个参数 指明,不然js可能会以8进制去转换。

4):布尔类型:
布尔在数字环境中:true 转换为 1 ,false 转换为 0 。
在字符环境中:true 转换为 “true” ,false 转换为 “false” 。
布尔转换:

var x_to_Boolean = Boolean(x); //方法1:使用Boolean ()函数。 
var x_to_Boolean = !x; //方法2:使用 感叹号。
Copy after login

5):函数的定义:
方法1:普通定义

function square(x){ 
      return x*x; 
}
Copy after login

方法2:函数直接量定义

var square = function(x){  return x*x; }   //推荐使用
Copy after login


方法3:构造参数

var square = new Function(“x”,”return x*x;”);   //效率低
Copy after login



6):对象:
如果有一个名为 cssrain 的对象 , 他有一个高度height的属性。
那么我们可以这么引用:

cssrain.height;
Copy after login


还可以使用关联数组定义:cssrain[“height”];

创建对象:
方法1:

var point = new Object(); 
point.x = 3; 
point.y = 5;
Copy after login

方法2:使用对象直接量
var point = {x:3 , y:5 }
当然json也可以咯。

对象在字符的环境下,会调用toString()方法。
数字环境下,会调用valueOf()方法。
布尔环境下,非空对象为true;

7):数组:
常规数组:以非负整数做为下标。image[0]
关联数组:以字符做为下标。如:image[“width”]
js不支持多维数组,但数组里面可以嵌套数组。

创建数组:
方法1:

var a = new Array(); 
a[0] = “1”; 
a[1] = 2; 
a[2] = { x:1, y:3}; 
方法2:  
var  a  =  new Array(“1” , 2 , {x:1,y:3} );
Copy after login

注意下:如果只传了一个参数;比如var a = new Array(3);
那么它是表示:3个未定义元素 的 新数组。
方法3:使用数组直接量
var a =[“1” , 2 , {x:1 , y :3 }]; //注意外面的 括号 , 不是花 括号。

8):null和undefined:
null表示无值;
undefined : 使用一个并未声明的变量,或者使用了已经声明的变量但未赋值或者使用了一个并不存在的属性。
undefined==null
如果要区分:
可以使用=== 或者typeof运算符。

9 ,新手常遇到的疑惑:

var s =”you are right”; 
var b = s.substring(s.lastIndexOf(“ ”)-1 , s.length);
Copy after login

疑惑:s是对象还是字符串,为什么字符串会有方法呢?
回答:s是字符串。之所以有方法 ,是因为 string类型 有一个相应的对象类(String)。
同样数字和布尔都有相应的Number , Boolean类。
Js会内部进行相应的包装对象。String对象就替换了原始的字符串。

总结:
简单了介绍了js中的一些概念(词法结构) 和 数据类型(部分)。
好了,今天就说到这里, 明天我们 继续。^_^。

笔记我已给部分群员看过,有好的评论也有不好的评论。所以我郑重声明下:
这是我看了DOM编程艺术,悟透JavaScript,javascript权威指南5做的笔记,
我资质不深,不能写出高深的文章,
如果你觉得笔记写得不好,可以不看我以后的。这篇文章就当作浪费你几分钟。 

以上就是Javascript入门学习第一篇 js基础_基础知识的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template