An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
What is JavaScript? • JavaScript is a scripting language • JavaScript was designed to add interactivity to HTML pages • A JavaScript is usually embedded directly into HTML pages • JavaScript is an interpreted language (means that scripts execute without preliminary compilation) • Everyone can use JavaScript without purchasing a license • Are Java and JavaScript the Same?
JavaScript的组成 • ECMAScript是一种标准化的脚本程序语言规范,它定义了脚本语言的基本语法,但并不与任何浏览器绑定,因此,Flash中的ActionScript及Javascript都是ECMAScript的具体实现 JavaScript ECMAScript BOM DOM
What can a JavaScript Do? • JavaScript is used in millions of Web pages to improve the design, put dynamic text into an HTML page, read and write HTML elements, react to events, validate forms, detect browsers, create cookies, and much more. • JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Mozilla, Firefox, Safari, and Opera.
How to put a JavaScript Into an HTML page? • <body> • <script type="text/javascript"> • document.write ("Hello World!") • </script> • </body> • The code above will produce this output on an HTML page: • Hello World! Example Explained To insert a JavaScript into an HTML page, we use the <script> tag (also use the type attribute to define the scripting language). So, the <script type="text/javascript"> and </script> tells where the JavaScript starts and ends:
The word document.write is a standard JavaScript command for writing output to a page. • By entering the document.write command between the <script type="text/javascript"> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page:
JavaScript Where To ... • JavaScripts in the body section will be executed WHILE the page loads. • JavaScripts in the head section will be executed when CALLED or when an event is triggered, go in the head section. • When you place a script in the body section, you will ensure that the script is loaded before anyone uses it.
JavaScript变量 • JavaScript的变量是一种弱类型变量,所谓弱类型变量是指它的变量无特定类型,定义任何变量都是用“var”关键字,并可以将其初始化为任何值,而且可以随意改变变量中所存储的数据类型,当然为了程序规范应该避免这样操作 • var name ="Six Hang"; • var age=28; • var school="CSU"; • var male=true; • 变量命名规范:第一个单词所有字母都小写,以后每个单词第一个字母大写,例如:sMyString
变量的命名原则 • 首字符必须是字母、下划线(_)或美元符号($); • 余下的字母可以是下划线。美元符号、任意字母或者数字; • 变量名不能是关键字或保留字 • 变量名对大小写敏感 • 变量名中不能有空格、回车符或其他标点字符 • 例如下面的变量名是非法的: var 5zhao; //数字开头,非法 var tang's; //对于变量名,单引号是非法字符 var this; //不能使用关键字作为变量名
赋值运算符 • Operator Example Is The Same As • = x=y x=y • += x+=y x=x+y • -= x-=y x=x-y • *= x*=y x=x*y • /= x/=y x=x/y • %= x%=y x=x%y
比较运算符 • Operator Description Example • == is equal to 5==8 returns false • === is equal to (checks for both value and type) x=5 y="5" x==y returns true x===y returns false • != is not equal 5!=8 returns true • > is greater than 5>8 returns false • < is less than 5<8 returns true • >= is greater than or equal to 5>=8 returns false • <= is less than or equal to 5<=8 returns true
逻辑运算符 • Operator Description Example • && and x=6 y=3 (x < 10 && y > 1) returns true • || or x=6 y=3 (x==5 || y==5) returns false • ! not x=6 y=3 !(x==y) returns true
字符串连接运算符 • A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator. • txt1="What a very" • txt2="nice day!" • txt3=txt1+txt2 • The variable txt3 now contains "What a verynice day!"
条件运算符 • variablename=(condition)?value1:value2 • greeting=(visitor=="PRES")?"Dear President ":"Dear " • If the variable visitor is equal to PRES, then put the string "Dear President " in the variable named greeting. If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.
字符串(String) • 字符串由零个或多个字符构成,字符可以是字母、数字、标点符号或空格。字符串必须放在单引号或双引号中。例如: • var course="data structure“ • var case='the birthday"19801106"' • 还可以使用转义字符(escaping)“\”实现特殊字符按原样输出: • var score = " run time 3\' 15\""
JavaScript中的转义字符 • Code Outputs • \' single quote • \" double quote • \& ampersand • \\ backslash • \n new line • \r carriage return • \t tab • \b backspace • \f form feed
字符串(String)对象常见属性 下面我们先定义一个示例字符串: • var myString = "This is a sample"; (1)length属性:它返回字符串中字符的个数,例如: var name="Six tang"; alert (name.length); //返回8 (2)charAt属性:它返回字符串对象在指定位置处的字符,第一个字符位置是0。例如: myString.charAt(2);//返回i (3)charCodeAt:返回字符串对象在指定位置处字符的10进制的ASCII码 myString.charCodeAt(2);//返回105 (4)indexOf:要查找的子串在字符串对象中的位置 myString.indexOf(“is”);//返回2
字符串(String)对象常见方法 (5)substr方法:截取字串 myString.substr(10,3);//返回sam,其中10表示位置,3表示长度 (6)substring方法:截取字串 myString.substring(5,9);//返回is a,其中5表示开始位置,9表示结束位置 (7)split方法:分隔字串到一个数组中 var a = myString.split(" "); //a[0] = “This” a[1]=“is” a[2]=“a” a[3]=“sample” (8)replace方法 myString.replace(“sample”,”apple”); //结果”This is a apple” (9)toLowerCase方法:变成小写字母 myString.toLowerCase();// this is a sample
数值型(number) • 在Javascript中,数值型数据不区分整型和浮点型,数值型数据和字符型数据的区别是数值型数据不要用引号括起来。例如下面都是正确的数值表示法, • var num1=23.45 • var num2=76 • var num3=-9e5 //科学计数法,即-900000 • alert(num1+ " "+ num2+" "+ num3);
布尔型(boolean) • 布尔型数据的取值只有两个:true和false。布尔型数据不能用引号引起来,否则就变成字符串了。用方法typeof()可以很清楚地看到这点,typeof()返回一个字符串,这个字符串的内容是变量的数据类型名称。 var married = true; document.write(typeof(married) +"<br />");// 输出boolean married = "true"; document.write(typeof(married)); // 输出string
数组(array) • 数组使用关键字Array来声明,同时还可以指定这个数组元素的个数,也就是数组的长度(length),例如: • var rank =new Array(12); //论坛的用户共分12级 • var Map =["China", "USA", "Britain"]; • var myColor= new Array(); • myColor[0]= "blue"; • myColor[1]= "yellow"; • myColor[2]= "purple"; • myColor[3]= "red";
对象的创建 • 使用new运算符和内部构造函数object()来创建一个空对象 • var Car= new object(); • 在JavaScript中给对象创建新的实例采用new关键字,如下所示: • var myCar = new Car(); • 这样就创建了一个Car对象的新实例myCar,通过这个实例就可以利用Car的属性、方法来设置关于myCar的属性或方法了,代码如下: • myCar.brand=Fiat; • myCar. accelerate(3);
this关键字 • 在JavaScript的对象系统中,this关键字常用于两种地方: (1)在构造器函数中,指代新创建的对象实例; (2)在对象的方法被调用时,指代调用该方法的对象实例。 for (… in …)语句可以遍历对象的所有属性和方法,例3-5所示的代码就遍历了Car对象的属性和方法,如果是属性则输出属性值,如果是方法则执行方法。
时间日期:Date对象 • <script language="javascript"> var myDate1 = new Date(); //运行代码前的时间 for(var i=0;i<3000000;i++); var myDate2 = new Date(); //运行代码后的时间 alert(myDate2-myDate1); • </script>
数学计算:Math对象 • Math对象也是JavaScript的一个内置对象,不需要由函数创建, random方法:该方法可以用来生成随机数,它返回一个0~1之间的随机数,不包括0和1。 • var iNum=Math.floor(Math.random()*100+1); • 这样iNum将随机产出一个1~100之间的整数(包括1和100)。