Professional Documents
Culture Documents
javascript快速教程 PDF
javascript快速教程 PDF
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
window
document
form
HistoryNavigator
JavaScript
JavaScript
JavaScript
Java ---
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
JavaScript (Object)(Event Driven)
1
JavaScript
2 OOP
JavaScript
JavaScript JavaScript
3
JavaScript Java
Java
weak type
JavaScript
interactive
5
JavaScript Web
event-driven
JavaScript
JavaScript
Java ---
www.javakc.com
TEL:01062972039
Java Javascript
1
Java
JavaScript
Object BasedEvent Driver
2
Java
JavaScript
interpreting
Java
JavaScript
characteristics of
scripting language
JavaScript HTML
HTML
5
HTML JavaScript <Script>...</Script>
Java <applet>...</applet> java
6
dynamic binding +
static binding
Java Java
JavaScript JavaScript
dynamic binding
Java ---
www.javakc.com
TEL:01062972039
JavaScript
header
1JavaScript HTML
head body
<script language="javascript">
document.write("<font color=blue>Hello World!</font>");
</script>
SRC="xxx.js"></script>
</head>
<body>
<form>
<input type=button onclick=show()>
</form>
</body>
.js javascript
function show(){
document.write("hello world!");
function
JavaScript
""''
true false
null
Java ---
www.javakc.com
TEL:01062972039
+
JavaScript varintdoubledelete
thisVariable
var x, myString;
declare
var y=100, st="hello";
cost=23.6;
JavaScript
abstract
char
double
float
import
native
public
synchronized
try
boolean
class
else
for
in
new
return
this
var
break
const
extends
function
instanceof
null
short
throw
void
byte
continue
false
goto
int
package
static
throws
while
case
default
final
if
interface
private
super
transient
with
catch
do
finally
implements
long
protected
switch
true
JavaScript
<script language="javascript">
var quanju=100;
function get(){
var i=10;
if(true){
var j=1;
}
}
</script>
1
123 512
2
Java ---
www.javakc.com
TEL:01062972039
3.1412.43
5E74e5
3
true false
4
'"'a'
"hello"
same as Python
JavaScript \
'\r''\t'
5
JavaScript null
null
C convention
assignment
x=100;
str="";
t=null;
a,
a*2/3,
(x+y)*10+(z-3)*20,
x==y
1if
if (){
;
Java ---
www.javakc.com
TEL:01062972039
}
x=80;
if (x>=60){
document.write( "");
}
if (){
1 ;
}else{
2 ;
}
if (x>=0){
y=x;
}else{
y=-x;
}
2switch
switch() {
case 1 1; break;
case 2 2; break;
case 3 3; break;
default: 4;
}
<script language="javascript">
function getWeek(week){
switch(week){
case 0:
st="";
break;
case 1:
st="";
break;
case 2:
st="";
Java ---
www.javakc.com
TEL:01062972039
break;
case 3:
st="";
break;
case 4:
st="";
break;
case 5:
st="";
break;
case 6:
st="";
break;
default:
st="";
}
return st;
}
</script>
3for
for (; ; )
//
}
4While
while() {
//
}
break switch
continue for while
Java ---
www.javakc.com
TEL:01062972039
JavaScript
1
function ([]) {
;
[return ;]
}
<script language="javascript">
//
function max(a,b){
var x;
if (a>b)
x=a;
else
x=b;
return x;
}
</script>
<script language="javascript">
max(20,30);
</script>
Java ---
www.javakc.com
TEL:01062972039
JavaScript
typeof typeof
undefined
<script language="javascript">
function get(a,b){
document.write("a"+typeof(a)+"<br>");
document.write("b"+typeof(b));
}
get(32.4);
</script>
*args
JavaScript
arguments arguments
.arguments[0]
<script language="javascript">
function getSum(){
var sum=0;
arguments
var number=getSum.arguments.length;//
for(var i=0;i<number;i++){
sum+=getSum.arguments[i];
}
list
return sum;
}
document.write("23+54+65="+getSum(23,54,65));
</script>
10
Java ---
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
1 eval( )
test=eval("x=8+9+5/2");
document.write(test);
// 19.5
2 escape()
% unicode
:
escape("Hello there")
"Hello%20there"
3unescape()
4parseFloat()
5parseInt(,radix)
radix parseInt()
JavaScript
11
Java ---
www.javakc.com
TEL:01062972039
Event
Event DriverEvent
Handler
1
JavaScript
FF
IE
onabort
onblur
onchange
onclick
ondblclick
onerror
onfocus
onkeydown
onkeypress
onkeyup
onload
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onreset
onresize
onselect
12
Java ---
www.javakc.com
TEL:01062972039
onsubmit
onunload
2
HTML HTML
button click MyProc()
<script language="javascript">
function myPorc(){
alert(document.all("txt").value);
}
</script>
<input type="text" name="txt" />
<input type="button" value="Try" onClick="myPorc( );"/>
<script language="javascript">
//mouseover
function imgover(){
document.myForm.img1.src="color.jpg";
}
//mouseout
by NAME
function imgout(){
document.myForm.img1.src="gray.jpg";
}
</script>
<form name="myForm">
<img border=0 name="img1" src="gray.jpg"
onmouseover="imgover();"
onmouseout="imgout()">
</form>
13
Java ---
www.javakc.com
TEL:01062972039
1 1 100
2 while 1 100
3 100 02468
4 1-100
5 if break 20 *
6x+y+z=50 x+2*y+5*z=100
7
*
**
***
****
n
8 a,b,c
if else
9
*
**
***
****
***
**
*
n
10
100 999
14
Java ---
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
.
.()
Array
[] 0
var myA=["","",""]; same as Python
document.write(myA);
myA[0]
//
default: all
myA[2]
(1)
(2) new
constructor
(3)
10
var anArray = new Array(9);
anArray[0]= "";
constructor overload
anArray[1]= "";
anArray[2]= "";
15
Java ---
www.javakc.com
TEL:01062972039
length
.length
myA.length 3
for myA
for(i=0;i<myA.length;i++){
alert(myA[i]);
}
join()
myA.join("/")
return a string
"//"
reverse()
myA.reverse( )
mutable
["","",""]
sort()
myA.sort ()
mutable
["","",""]
concat()
concat() a
<script type="text/javascript">
var a = [1,2,3];
document.write(a.concat(4,5));
//
//1,2,3,4,5
mutable
</script>
concat()
<script type="text/javascript">
var arr = ["George","John","Thomas"];
16
Java ---
www.javakc.com
TEL:01062972039
at the end
concat()
<script type="text/javascript">
var arr = ["George","John","Thomas"];
var arr2 = ["James","Adrew","Martin"];
var arr3 = ["William","Franklin"];
document.write(arr.concat(arr2,arr3));
//
overloaded
//George,John,Thomas,James,Adrew,Martin,William,Franklin
</script>
pop()
1
pop() undefined
<script type="text/javascript">
var arr = ["George","John","Thomas"];
document.write(arr);
document.write("<br />");
document.write(arr.pop());
document.write("<br />");
document.write(arr);
//
//George,John,Thomas
//Thomas
//George,John
</script>
push()
mutable + return
arrayObject.push(newelement1,newelement2,....,newelementX)
17
Java ---
www.javakc.com
TEL:01062972039
System.out.println(obj+"...")
//
//George,John,Thomas
//4
//George,John,Thomas,James
</script>
shift() queue
shift() undefined
arrayObject
<script type="text/javascript">
var arr = ["George","John","Thomas"];
document.write(arr + "<br />");
document.write(arr.shift() + "<br />");
document.write(arr);
//
//George,John,Thomas
//George
//John,Thomas
</script>
unshift()
queue
unshift() arrayObject
0
1
unshift()
<script type="text/javascript">
18
Java ---
www.javakc.com
TEL:01062972039
//William,George,John,Thomas
</script>
slice(start,end)
<script type="text/javascript">
var arr = ["George","John","Thomas"];
document.write(arr + "<br />");
document.write(arr.slice(1) + "<br />");
document.write(arr);
//
//George,John,Thomas
//John,Thomas
//George,John,Thomas
</script>
<script type="text/javascript">
var arr =["George","John","Thomas","James","Adrew","Martin"];
document.write(arr + "<br />");
document.write(arr.slice(2,4) + "<br />");
document.write(arr);
//
//George,John,Thomas,James,Adrew,Martin
//Thomas,James
//George,John,Thomas,James,Adrew,Martin
</script>
splice()
DRY
arrayObject.splice(index,howmany,element1,.....,elementX)
19
Java ---
www.javakc.com
TEL:01062972039
splice() index
index
howmany
"0"
index
element1
index
elementX
<script type="text/javascript">
var arr =["George","John","Thomas","James","Adrew","Martin"];
document.write(arr + "<br />");
arr.splice(2,0,"William");
document.write(arr + "<br />");
//
//George,John,Thomas,James,Adrew,Martin
//George,John,William,Thomas,James,Adrew,Martin
</script>
index 2
<script type="text/javascript">
var arr =["George","John","Thomas","James","Adrew","Martin"];
document.write(arr + "<br />");
arr.splice(2,1,"William");
replace
document.write(arr);
//
//George,John,Thomas,James,Adrew,Martin
//George,John,William,James,Adrew,Martin
</script>
index 2 ("Thomas")
("William")
<script type="text/javascript">
var arr =["George","John","Thomas","James","Adrew","Martin"];
20
Java ---
www.javakc.com
TEL:01062972039
from index a
delete b elements
insert some ele at a
//George,John,Thomas,James,Adrew,Martin
//George,John,William,Martin
</script>
document forms
<form name="firstForm"></form>
<form name="secondForm"></form>
<form name="thirdForm"></form>
<script language="javascript">
var fs=document.forms;
for(i=0;i<fs.length;i++){
alert(fs[i].name);
}
</script>
options
<form name="myForm">
<select multiple size="5" name="box" style="width:150" onClick="f(this);">
<option value="apple"></option>
<option value="orange"></option>
<option value="banana"></option>
</select>
</form>
<script language="javascript">
function f(o){
for(i=0;i<o.options.length;i++){
alert(o.options[i].value+","+o.options[i].text);
}
}
</script>
21
Java ---
www.javakc.com
TEL:01062972039
String
myStr="";
myStr.length 9
(1)
big(),
bold(),
(2)
javascript
presentation logic
str="Hello";
anchor()
HTML
str.anchor("b"):<A NAME=b>Hello</A>
big()
str.big() : <BIG>Hello</BIG>
blink()
str.blink() : <BLINK>Hello</BLINK>
bold()
str.bold() : <B>Hello</B>
charAt()
"".charAt(2)""
"".charAt(0)""
charCodeAt()
Unicode
"".charCodeAt(2) 27426
concat()
"".concat("")""
fixed()
str.fixed() : <FIXED>Hello</FIXED>
fontcolor()
fontsize()
indexOf()
"".indexOf("") 2
22
Java ---
www.javakc.com
TEL:01062972039
"".indexOf("")-1
"".indexOf("") 2
italics()
lastIndexOf()
"".lastIndexOf("") 6
link()
str.link("http://www.javakc.com") :
<a href="http://www.javakc.com"> Hello
</a>
match()
replace()
search()
slice()
"".slice(2,4) ""
small()
str.small():<SMALL>Hello</SMALL>
split()
"||".split("|")
{"","",""}
strike()
str.strike():<strike>Hello </strike>
sub()
substr()
"".substr(1,2) ""
substring()
"".substring(2,4)""
sup()
toLowerCase()
"Welcome to BeiJing".toLowerCase()
welcome to beijing
toUpperCase()
"Welcome to BeiJing".toUpperCase()
WELCOME TO BEIJING
23
Java ---
Math
www.javakc.com
TEL:01062972039
Math.
Math.
Math.E
2.718
Math.PI
3.14159
Math
abs(x)
acos(x)
asin(x)
atan(x)
-PI/2 PI/2 x
atan2(y,x)
ceil(x)
cos(x)
exp(x)
floor(x)
log(x)
max(x,y)
x y
min(x,y)
x y
pow(x,y)
x y
random()
0 ~ 1
round(x)
sin(x)
sqrt(x)
tan(x)
24
Java ---
www.javakc.com
toSource()
valueOf()
Math
TEL:01062972039
Date
new Date()
new Date()
1970 1 1
new Date(2005,6,3,21,0,22)
2005 7 3 0
getDate()
getDay()
getHours()
getMinutes()
getSeconds()
getTime()
1970 1 1
getYear()
setDate()
setDay()
setHours()
setMinutes()
setSeconds()
setTime()
1970 1 1
setYear()
0 1 6
0 1 6
25
Java ---
www.javakc.com
TEL:01062972039
1
2
3
4
5 50 100 60
6
26
Java ---
www.javakc.com
window
TEL:01062972039
object
Window
closed
defaultStatus
document
Document Document
history
History History
innerheight
innerwidth
length
location
name
Navigator
opener
Location Location
Navigator Navigator
outerheight
outerwidth
pageXOffset
pageYOffset
parent
27
Java ---
Screen
self
www.javakc.com
Screen Screen
Window
status
top
window
screenLeft
screenTop
screenX
screenY
TEL:01062972039
window self
x y IESafari
Opera screenLeft screenTop Firefox Safari
screenX screenY
Window
alert()
blur()
clearInterval()
setInterval() timeout
clearTimeout()
setTimeout() timeout
close()
confirm()
createPopup()
pop-up
focus()
moveBy()
moveTo()
open()
print()
prompt()
resizeBy()
resizeTo()
28
Java ---
www.javakc.com
TEL:01062972039
scrollBy()
scrollTo()
setInterval()
setTimeout()
1
open
window.open(URL,,)
close()
Open
toolbar=yes, width=500,height=200
2
alert ()
29
Java ---
www.javakc.com
TEL:01062972039
<html>
<head>
<script type="text/javascript">
function disp_confirm(){
var r=confirm("");
if (r==true){
document.write("");
}else{
document.write("");
}
}
</script>
</head>
<body>
<input type="button" onclick="disp_confirm()" value="" />
</body>
</html>
prompt()
<html>
<head>
<script type="text/javascript">
function disp_prompt(){
var name=prompt("","Bill Gates");
if (name!=null && name!=""){
document.write("" + name + "");
}
}
</script>
</head>
<body>
<input type="button" onclick="disp_prompt()" value="" />
</body>
meaningless
</html>
30
Java ---
www.javakc.com
TEL:01062972039
00
moveBy
moveBy(10,10)
<html>
<head>
<script type="text/javascript">
function moveWin(){
myWindow.moveBy(50,50);
myWindow.focus();
}
</script>
popup
</head>
<body>
<script type="text/javascript">
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("This is 'myWindow'");
</script>
<input type="button" value="Move 'myWindow'" onclick="moveWin()" />
</body>
</html>
moveTo
moveTo(100,100)
<html>
<head>
<script type="text/javascript">
function moveWin(){
myWindow.moveTo(0,0);
myWindow.focus();
}
</script>
</head>
31
Java ---
www.javakc.com
TEL:01062972039
<body>
<script type="text/javascript">
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("This is 'myWindow'");
</script>
<input type="button" value="Move 'myWindow'" onclick="moveWin()" />
</body>
</html>
setInterval
clearInterval
()
<html>
<body>
<input type="text" id="clock" size="35" />
<script language=javascript>
var int=self.setInterval("clock()",50);
function clock(){
var t=new Date();
document.getElementById("clock").value=t;
}
</script>
<button onclick="int=window.clearInterval(int)">
Stop interval
</button>
</body>
</html>
setTimeout
clearTimeout
<html>
<head>
<script type="text/javascript">
function timedMsg(){
var t=setTimeout("alert('5 seconds!')",5000);
}
32
Java ---
www.javakc.com
TEL:01062972039
</script>
</head>
<body>
<form>
<input type="button" value=""
onClick = "timedMsg()">
</form>
<p>5 </p>
</body>
</html>
window.focus()
<html>
<body>
<script type="text/javascript">
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("This is 'myWindow'");
myWindow.focus();
</script>
</body>
</html>
33
Java ---
DOM
www.javakc.com
TEL:01062972039
document
()
window
document
Window (HTML )
form
windowdocument history
document
document
document form form1
txtbox
document.form1.txtbox.value
34
Java ---
www.javakc.com
TEL:01062972039
HTML document
Document
all[]
HTML
anchors[]
Anchor
applets
Applet
forms[]
Form
images[]
Image
links[]
Area Link
Document
body
<body>
<frameset>
cookie
cookie
domain
lastModified
referrer
URL
title
URL
URL
Document
close()
document.open()
getElementById()
id
getElementsByName()
getElementsByTagName()
open()
document.write()
document.writeln()
35
Java ---
www.javakc.com
TEL:01062972039
write()
HTML JavaScript
writeln()
write()
document body
onkeydown
onkeypress
onkeyup
onclick
ondblclick
onmousedown
onmouseup
load
unload
attachEvent body
document.attachEvent ('onclick',getA);
36
Java ---
www.javakc.com
TEL:01062972039
form
1
<form> Web
Web
document.form1.submit();
37
Java ---
Name
action
target
www.javakc.com
TEL:01062972039
(_blank,_parent,_self,_top)
()
encoding
text/html
method
GetPost
elements
,0,1,
document..elements.length
elements elements
HTML <form>
4
<script type="text/javascript">
function display(){
var str;
//
str="name="+document.form1.name+"\r";
str=str+"action="+document.form1.action+"\r";
str=str+"method="+document.form1.method+"\r";
//elements
str=str+"elements:"+"\r";
38
Java ---
www.javakc.com
TEL:01062972039
for(var i=0;i<document.form1.elements.length;i++){
str=str+ "
"+document.form1.elements[i].type
+"\t"+document.form1.elements[i].name
+"\t"+document.form1.elements[i].value+"\r";
}
window.alert(str);
return false;
}
function submitform(){
//+
document.forms[0].submit();
}
</script>
<form name="form1" action="/web/user" method="post" onsubmit="return
display()">
<input type="text" name="text1" value="AAA"/>
<input type="text" name="text2" value="BBB"/>
<input type="checkbox" name="check1" value="aaa"/>
<input type="submit" name="submit" value=""/>
<input type="button" name="button" onclick="submitform();" value="js"/>
</form>
onsubmit onsubmit
false submit
JavaScript
.elements[]
document.form1.check
document.form1.elements[2]
1text
text text
39
Java ---
www.javakc.com
TEL:01062972039
accessKey
Alt + accessKey
alt
defaultValue
disabled
id
maxLength
name
readOnly
size
tabIndex
id
tab
type
value
value
blur( )
select( )
focus()
onfocusonbluronselectonchange
text
<form name="form1">
<input type="text" name="text1" value="Welcome"/>
</form>
<script type="text/javascript">
document.form1.text1.value="this is a javascirpt";
document.form1.text1.select();
alert(document.form1.text1.value);
document.text1.blur();
</script>
40
Java ---
www.javakc.com
TEL:01062972039
2textarea
textarea textarea
<textarea name=""></textarea>
name
textarea
value
textarea
defaultvalue
blur( )
select( )
textarea
onfocusonbluronselectonchange
3select
select
<select name="" size="">
<option value=""></option>
<option value="" selected></option>
</select>
disabled
id
id
length
multiple
name
options
(option )
option
text
value
value
selected
41
Java ---
selectedIndex
size
options.add()
www.javakc.com
TEL:01062972039
blur()
focus()
remove()
onfocusonbluronchange
4button
button
<input type="button" value="" name="">
accessKey
alt
disabled
id
name
value
id
tabIndex
tab
blur()
click()
focus()
onclick
5checkbox
checkbox
42
Java ---
www.javakc.com
TEL:01062972039
accessKey
alt
checked
defaultChecked
disabled
checkbox
checked
checkbox
checkbox id
name
checkbox
value
checkbox
id
tabIndex
checkbox
checkbox tab
checkbox value
blur()
checkbox
click()
checkbox
focus()
checkbox
onclick
6radio
radio
radio radio
<input type="radio" value="" name="" checked>
<input type="radio" value="" name="" >
accessKey
alt
checked
defaultChecked
disabled
43
Java ---
www.javakc.com
id
id
name
TEL:01062972039
tabIndex
tab
value
value
blur()
click()
focus()
onclick
7hidden
hidden
<input type="hidden" value="" name="" >
alt
id
id
name
value
value
8submit
submit submit
<input type="submit" value="" name="" >
accessKey
alt
disabled
id
id
name
44
Java ---
tabIndex
value
www.javakc.com
TEL:01062972039
tab
blur()
click()
focus()
onclick
9password
password
<input type="password" value="" name="" >
accessKey
alt
defaultValue
disabled
id
id
maxLength
name
readOnly
size
tabIndex
tab
value
value
blur()
click()
focus()
onfocusonbluronselectonchange
45
Java ---
www.javakc.com
TEL:01062972039
History Navigator
History
History URL
History window window.history
History
length
URL
History
back()
history URL
forward()
history URL
go(number|URL)
history
-1
Navigator
Navigator
Navigator Window navigator
Navigator
plugins[]
Plugin Plug-in
MIME
plugins[] IE 4 IE 4 IE
4 Plugin
Navigator
appCodeName
46
Java ---
www.javakc.com
TEL:01062972039
appMinorVersion
appName
appVersion
browserLanguage
cookieEnabled
cookie
cpuClass
CPU
onLine
platform
systemLanguage
OS
userAgent
user-agent
userLanguage
OS
Navigator
IE
javaEnabled()
Java
taintEnabled()
(data tainting)
47
Java ---
www.javakc.com
TEL:01062972039
JavaScript
HTML <iframe><frame>
HTML
JavaScript
HTML window
window.frames
document
window.frames["frameName"]
window.frames.frameName
window.frames[index]
window.frames["frameName"]
self.frames["frameName"]
self.frames[0]
frames[0]
frameName
HTML
window window
window.document window.location
window.frames["frameName"]
frameName
48
Java ---
www.javakc.com
TEL:01062972039
window
window.frames["frameName"].frames["frameName2"]
2
window parent
window.parent TestA.html
<html>
<head><title></title></head>
<body>
<form name="formA" id="formA">
<input type="text" name="username" id="username"/>
</form>
<iframe src="TestB.html" width="100%"></iframe>
</body>
</html>
iframe TestB.html
TestB.html TestA.html username TestB.html
<script type="text/javascript">
var parentWin=window.parent;
parentWin.formA.username.value="javakc";
</script>
2
<frameset rows="30%,70%">
<frame src="left.html" name="frame1"/>
<frame src="main.html" name="frame2"/>
</frameset>
frame1 frame2
self.parent.frames["frame2"]
49
Java ---
www.javakc.com
TEL:01062972039
window
self.parent.frames["childName"].frames["targetFrameName"]
5
parent window top
top.window.formA.username.value="javakc";
if(self==top){
//too do
}
window.frames[0].location="index.html";
index.html
<frameset rows="30%,70%">
<frame src="left.html" name="frame1"/>
<frame src="main.html" name="frame2"/>
</frameset>
<a href="frame1.location='index.html';frame2.loaction='top.html'">link</a>
JavaScript
<script type="text/javascript">
function hello (){
alert("hello javakc");
}
window.hello();
</script>
Java ---
www.javakc.com
TEL:01062972039
window
var a="javakc";
alert(window.a);
javakc
window
<frameset rows="30%,70%">
<frame src="left.html" name="link"/>
<frame src="main.html" name="show"/>
</frameset>
left.htm JavaScript
<script type="text/javascript">
var arrCars=new Array();
function addToCars(id){
arrCars.push(id);
}
</script>
main.html
<a href="void(0)" onclick="self.parent.link.addToCars('id')"></a>
51
Java ---
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
1 try-catch-finally
catch
try{
//
}catch(e){
//
}finally{
//
}
java C#
Ajax XMLHttpRequest
<script type="text/javascript">
var xmlhttp;
try{
//IEXMLHttpRequest
xmlhttp=new ActionXObject("Microsoft.XMLHTTP");
}catch(e){
try{
//IEXMLHttpRequest
xmlhttp=new XMLHttpRequest();
}catch(e){
}
}
</script>
XMLHttpRequest
52
Java ---
www.javakc.com
TEL:01062972039
2 throw
JavaScript
throw throw value
vlaue JavaScript
JavaScript e error new
Errormessage error message
<script type="text/javascript">
//
function sum(a,b){
a=parseInt(a);
b=parseInt(b);
//ab
if(isNaN(a) || isNaN(b)){
throw new Error("");
}
return a*b;
}
try{
//
var s=sum(1,"a");
}catch(e){
//
alert(e.message);
}
</script>
sum
catch alert
53
Java ---
www.javakc.com
TEL:01062972039
JavaScript
JavaScript
JavaScript
<script language="javascript">
function HelloClass{
alert("");
//
HelloClass(v){
alert("");
}
}
new HelloClass();
</script>
HelloClass
new
new Date();
var obj=new HelloClass();
Date
JavaScript new
javaScript
():
54
Java ---
www.javakc.com
TEL:01062972039
[""]
//()
var len=arrs["length"];
//()
alert(len);
//()
var len=arrs.length;
//()
alert(len);
JavaScript
<script language="javascript">
//User,name,agesex,
function User(){
this.name="Tom";
this.age=18;
this.sex="";
}
//User
var user=new User();
//
function show(slt){
if(slt.selectedIndex!=0){
alert(user[slt.value]);
}
}
</script>
<!-- -->
<select onchange="show(this)">
<option value="name"></option>
<option value="age"></option>
55
Java ---
www.javakc.com
TEL:01062972039
<option value="sex"></option>
</select>
User value
prototype
prototype
function prototype Prototype
prototype new prototype
prototype
<script language="javascript">
//
function HelloClass(){
}
//prototypemethod
HelloClass.prototype.method=function(){
alert("prototype");
}
var obj=new HelloClass(); //HelloClass
obj.method();
//objmethod
</script>
new prototype
<script language="javascript">
//
function HelloClass(){
this.name="javakc";
}
//prototype
HelloClass.prototype.showName=function(){
alert(this.name);
}
var obj=new HelloClass(); //HelloClass
//prototypeshowName
obj.showName();
56
Java ---
www.javakc.com
TEL:01062972039
</script>
JavaScript
C# Java
JavaScript for(in)
<script language="javascript">
for(var vs in obj){
//
if(typeof(obj[vs])=="function"){
obj[vs]();
//
}else{
alert(obj[vs]);
}
}
</script>
var vs obj
obj
JavaScript
JavaScript
prototype
57
Java ---
www.javakc.com
TEL:01062972039
JavaScript
prototype
<script language="javascript">
function HelloClass(){
//
}
function HelloSubClass(){
//
}
HelloSubClass.prototype=HelloClass.prototype;
HelloSubClass.prototype.Propertys="name";
HelloSubClass.prototype.subMethods=function(){
//
alert("in Methods");
}
var obj=new HelloSubClass();
obj.subMethods();
</script>
JavaScript property
HelloSubClass HelloClass HelloSubClass method
<script language="javascript">
//HelloClass
function HelloClass(){
//
}
//HelloClass
HelloClass.prototype={
f1:function(){
alert("in HelloClass");
58
Java ---
www.javakc.com
TEL:01062972039
}
}
//HelloSubClass
function HelloSubClass(){
//
}
//HelloSubClassHelloClass
HelloSubClass.prototype=HelloClass.prototype;
//HelloSubClassmethod
HelloSubClass.prototype.Propertys="name";
HelloSubClass.prototype.method=function(){
//
alert("in HelloSubClass");
}
//
var obj1=new HelloClass();
var obj2=new HelloSubClass();
//method
obj1.method();
obj2.method();
</script>
HelloSubClass
HelloSubClass prototype HelloClass prototype
HelloSubClass prototype HelloClass
HelloClass HelloSubClass
HelloClass HelloSubClass prototype
JavaScript
HelloSubClass prototype
HelloClass prototype
instanceof prototype
obj1 obj2 prototype instanceof
prototype
prototype
prototype
for(in) prototype
59
Java ---
www.javakc.com
TEL:01062972039
prototype
<script language="javascript">
//HelloClass
function HelloClass(){
//
}
//HelloClass
HelloClass.prototype={
method:function(){
alert("in HelloClass method");
},
method2:function(){
alert("in HelloClass method2");
}
}
//HelloSubClass
function HelloSubClass(){
//
}
//HelloSubClassHelloClass
for(var p in HelloClass.prototype){
HelloSubClass.prototype[p]=HelloClass.prototype[p];
}
//HelloClassmethod
HelloSubClass.prototype.method=function(){
//
alert("in HelloSubClass method");
}
//
var obj1=new HelloClass();
var obj2=new HelloSubClass();
//method
obj1.method();
obj2.method();
//method2
obj1.method2();
obj2.method2();
</script>
60
Java ---
www.javakc.com
TEL:01062972039
//inherit
Function.prototype.inherit=function(baseClass){
for(var p in baseClass.prototype){
this.prototype[p]=baseClass.prototype[p];
}
}
Function
inherit
//HelloSubClassHelloClass
HelloSubClass.inherit(HelloClass);
61
Java ---
www.javakc.com
TEL:01062972039
1. A
A. var obj = ( );
B. var obj = [ ];
C. var obj = { };
D. var obj = //;
2. javascript B
A. with
B. parent
C. class
D. void
3. C
A. null instanceof Object
B. null === undefined
C. null == undefined
D. NaN == NaN
4. javascript (ABCD)
A.JScript javascript
B.javascript Java Java
C.FireFox IE javascript
D.AJAX javascript
5. foo att att ACE
A. foo.att
B. foo("att")
C. foo["att"]
D. foo{"att"}
E. foo["a"+"t"+"t"]
62
Java ---
www.javakc.com
TEL:01062972039
6. HTML AB
A.<TEXTAREA></TEXTAREA>
B.<INPUT type="text"/>
C.<INPUT type="hidden"/>
D.<DIV></DIV>
7. javascript ABC
A.escape
B.parseFloat
C.eval
D.setTimeout
E.alert
8. IFrame (ABCD)
A. IFrame
B. IFrame
C. IFrame
D. IFrame
9. ABCDE
A. TBODY
B. CAPTION
C. TBODY
D. COLGROUP
E. COL
10. IE window ACD
A.window.opener window
B.window.reload()
C.window.location="a.html" window.location.href="a.html"
a.html
D. g window.g
11. select
12. JavaScript foo()
13. form input readonly disable
63