JavaScript引入方式

JavaScript引入方式

內嵌

<script type = "text/javascript">
    document.write('hello,javascript!');
</script>
兩個<script></script>代碼塊錯誤,不會互相影響。
<script type = "text/javascript">
    document.write(hello);
    document.write('這裏不能被執行!');
</script>
<script type = "text/javascript">
    document.write('這裏還可以執行');
</script>

引用文件

使用引用文件方式時,在代碼塊中的代碼就不會被運行。

<script type="text/javascript" src="js文件位置">這裏不會被執行</script>

注:開發方式一般都是結構(html)、行爲(JavaScript)、樣式(css)相分離,所以多采用引用文件方式。

JavaScript基本語法

變量(variable)

<script type = "text/javascript">
    var a;                    //聲明變量
    var b,
        c = 0,
        d = true;             //同時聲明多個變量並可賦初始值
    a = 'hello,javascript!';  //變量賦值
    document.write(a);        //使用變量
</script>

變量類型

原始值:Number、String、Boolean、undefined、null

<script type = "text/javascript">
  var a = 10;//變量的類型按賦值的類型決定的,a 爲Number。
  var b = a; //變量的類型按賦值的類型決定的,a 也爲Number。
  a = 20;
  document.write("a的值:" + a + "<br/>");
  document.write("b的值:" + b + "<br/>");
  //a的值:20
  //b的值:10
  //原始值變量存儲空間是相互獨立的,修改 a 變量的值,不會影響 b 變量的值。
</script>

引用值:Object、Array(數組)、function(函數)等。

引用類型的變量存儲的是指向存儲空間的地址,多個引用類型的變量可以指向相同的存儲空間。

相當於一個人(存儲空間)有多個聯繫方式(引用值變量),通過不同的聯繫方式(引用值變量)都可以找到這個人(存儲空間)。

<script type = "text/javascript">
  var a = [1,2,3,4];//變量的類型按賦值的類型決定的,a 爲Array。
  var b = a;        //變量的類型按賦值的類型決定的,b 爲Array並與 a 指向相同的存儲空間。
  a.push(5);        //通過變量 a 向存儲空間添加數據。
  document.write("a的值:" + a + "<br/>");
  document.write("b的值:" + b + "<br/>");
  //a的值:1,2,3,4,5
  //b的值:1,2,3,4,5
  //引用值變量存儲的是存儲空間的地址,修改變量 a 的值,也就是修改了存儲空間中的值,因爲 b 的存儲空間與 a 相同所以值也變更。
</script>

變量命名規則

變量首字母以英文字母、_(下劃線)、$ 開頭。

變量可以包含英文字母、_(下劃線)、$ 、數字。

不能以JavaScript的關鍵字、保留字命名變量。

算術運算符

+ - * / % ++ –

“+” 作用:數字相加運算、字符串連接,任何數據類型 “+” 字符串都會轉成字符串。

()作爲一個數學運算中的基本符號,在計算機程序中也被大量使用,最基本的就是提高運算優先級。

賦值運算符

*= += -= = /= %=

賦值運算符優先級最低

<script type = "text/javascript">
    //運算符運用,確定不了優先級時,用()提升表達式優先級(推薦)。
	var d = 10;
	var e = 10;
	document.write("++d:" + (++d) + "<br/>");
	document.write("e++:" + (e++) + "<br/>");
	document.write("d:" + d + " - " + "e:" + e + "<br/>"); 
	
	var a = 10;
	var b = ++a - 1 + a++;
	document.write("b:" + b + " - " + "a:" + a + "<br/>"); 
 
	var c = 10;
	c += 10 + 1;
	document.write("c:" + c);
</script>
<script type = "text/javascript">
    //交換兩個數
	var a = 123;
	var b = 234;
	a = a + b;
	b = a - b;
	a = a - b;
	document.write("a:" + a + " b:" + b);
</script>

比較運算符

== === != < > <= >=

<script type = "text/javascript">
	var sp = ",";        //輸出時用於分割
	var a = "10" > "2";  //字符串順序比較各位字符的ASCII碼
	var b = 10 > 2;
	var c = 10 == 2;
	var d = 10 != 2;
	var e = Infinity == Infinity;  //無窮大比較
	var f = NaN == NaN;            //非數比較特殊,自己不等於自己
	var g = null == null;
	var h = undefined == undefined;
	document.write(a,sp,b,sp,c,sp,d,sp,e,sp,f,sp,g,sp,h);
</script>

邏輯運算符

&& || !

<script type = "text/javascript">
	//邏輯運算符&&:左面表達式如果爲真,返回右面表達式的值,否則返回左面表達式的值。
	//undefined, null, NaN, "", 0, false 都爲假;
	var sp = ",",
		a = 1 && 2,
		b = 1 && 0,
		c = 1 && true,
		d = 1 && false,
		e = 0 && 2,
		f = 0 && 0,
		g = 0 && true,
		h = 0 && false; 
	document.write(a,sp,b,sp,c,sp,d,sp,e,sp,f,sp,g,sp,h);
	//邏輯運算符||:左面表達式如果爲真,返回左面表達式的值,否則返回右面表達式的值。
		a = 1 || 2;
		b = 1 || 0;
		c = 1 || true;
		d = 1 || false;
		e = 0 || 2;
		f = 0 || 0;
		g = 0 || true;
		h = 0 || false;
	document.write(a,sp,b,sp,c,sp,d,sp,e,sp,f,sp,g,sp,h);
	//邏輯運算符!:把表達式轉成Boolean類型並取反。
	document.write(!a,sp,!b,sp,!c,sp,!d,sp,!e,sp,!f,sp,!g,sp,!h);
</script>

位運算符

略:基本不用

條件運算符

? : (三元運算符)

<script type = "text/javascript">
	var a = 8;
	var b = 2;
	document.write(a > b ? a - b : b - a);
	//如果 a 大於 b,則執行 a - b,否則執行 b - a。
</script>

————————————————
版權聲明:本文爲CSDN博主「じоνё靁〃」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/jingliuting/article/details/86571548

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章