Javascript Statement(上)

Posted notesForKai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript Statement(上)相关的知识,希望对你有一定的参考价值。

javascript Statement(上)


一、JS基础知识

1. js引用

<!--Mathod 1-->
<head>
	<script src="js_path"></script>
</head>

<!--Mathod 2-->
<body>
	<script>
		statement
	</script>
</body>

2. Javascript构成

  • ECMASript: 核心部分,包含js的语法
  • DOM: 文件对象模型 (整合html, CSS, JS)
  • BOM: 浏览器模型 (整合JS和浏览器)

image

3. 定义变量

如果不用var声明,则该变量默认为全局变量

var a=1;
var a=1, b=2;
var s="hello",f=3.2;

4. 变量命名规则

  • 只能以字母、下划线、$美元符开头,且区分大小写
  • Camel标记法:首字母小写,其他单词以大写字母开头
  • Pascal标记法:每个单词的首字母都大写
  • 匈牙利标记法:在Pascal标记的前提下,首字母需为小写的数字类型(如:iMyFirstArg; sMySecondArg)

5. 注释方式

  • 单行注释://
  • 多行注释:/*content*/

6. 常量和标识符

自定义标识符: sayHello
字符串常量: "nihao"
保留字: function; var

<script>
	function sayHello(){
		var sHelloFirstArg="nihao"
		document.write(sHelloFirstArg)
	}
	sayHello();
</script>

二、JS数据类型

1. 基本数据类型

image

2. 数据类型的转换

字符串拼接引起的自动转换

  • 数字+字符串→字符串
  • 数字+布尔值→数字(true→0;false→1)
  • 布尔值+字符串→字符串(true→"true";false→"false")

转换函数

NaN: 当字符串转换数字失效时,会得到NaN这个值,归属于Number类型。但NaN是一个空值,不会和任何数据包括它自己相等

parseInt();		//将数据转换成整型
typeof();		// 返回数据类型

三、JS运算符

1. 算数运算符

// 基本运算符(+ - * / ++ --)
var a = 1;
var b = a++		// b=1
var b = ++a		// b=2

2. 一元运算符

If "b" is the string of digital type, it will be automatically turned back the "b" of digital type but will get a NAN value.

var a = +b
var a = -b

3. 逻辑运算符

"&&" and "!!" can get a value not belonging the boolen type, if the result on one side is not the type of boolen.

// && !! !
2>1 && 3>5		// false(Equate "and" in python)
2>1 !! 3>5		// true(Equate "or" in python)
2 ! [3,5]		// true(Equate "not" in python)

4. 位运算符

&: only when the two number are 1, the counting result equate 1,but 0 in binary system\'s counting
|: the result of same number is itself. but 1 in binary system\'s counting
^: the result of same number is 0. but 1 in binary system\'s counting

// all numbers shift two digits westwards, and the empty position will be filled by zero in the binary system
<<

// all numbers shift two digits eastwards, and the empty position will be filled by zero in the binary system
>>

// all zero and one exchange position for all numbers
~

// 位与(&)
1 & 2 = 0
/*
0 1
1 0
*/

// 位或(|)
1 | 2 = 3
/*
0 1
1 0
*/

// 异或(^)
1 ^ 2 = 3
/*
0 1
1 0
*/

5. 等性运算符

notice
null == undefined return ture
before checked if two characters are equation, all counting numbers tend to become the type of Number,having a priority of switch.

// one of the two characters Will change type if the two characters are the type that can be switched by each other,then return true or false
==
!=

// one of the two characters Will not change type, then return true or false
===
!==

6. 关系运算

// > < >= <=
alert(25<3)				// false
alert("25"<"3")			// true (Each characters base on the position on the ASCI to judge, if the comparation have not the result)
alert("25"<3)			// false

7. 布尔值为false的值

flase;
[];
0;
null;
undefined;

四、循环判断语句

1. if语句

if (condition){
	statement;
}else if(condition){
	statement;
}else{
	statement;
}

2.switch选择控制语句

var x = 3;
switch(x){
	case 1:statement; break;
	case 2:statement; break;
	case 3:statement; break;
	default:statement;
}

3. for 语句

// Method 1
var a=[1,"hello",true];
for (var i in a){
	console.log(i);
	console.log(a[i]);
}

// Method 2
for (var i=1; i<10; i=i+1){
	console.log(i)
}

4. while 循环

var sum = 0;
var flag = 1;
while(flag<101){
	sum += flag;
	++flag;
}

5. 异常处理

throw Error("xxx") (Artificially thorw the information of error)

try{
	statement;
}
catch(e){
	statement;
}
finally{
	statement;
}

以上是关于Javascript Statement(上)的主要内容,如果未能解决你的问题,请参考以下文章

javascript if(-1) ...statement... 计算结果为 true [重复]

PDO Prepared Statement 允许执行 javascript

如何在 JavaScript 中编写 Python print(statement, end=' ')? [复制]

javascript 使用for ... in Statement中的对象的键迭代

JavaScript:return报错 Uncaught SyntaxError: Illegal return statement

JavaScript:return报错 Uncaught SyntaxError: Illegal return statement