找回密码
 注册
搜索
热搜: 回贴

JavaScript类和内置对象

2009-12-16 02:17| 发布者: admin| 查看: 19| 评论: 0|原作者: 小寳寳

Javascript 对象 和类

Javascript是基于对象而非面向对象的程序设计语言,它不支持类和继承,Javascript在面向对象方面与Java的严谨相比,显得非常随便。


创建对象


Javascript通过实现包含属性说明和方法定义的构造方法来声明类,并没有专门的class类定义。

如:用来声明Oven类的构造方法

function Oven(color,type,time)

{this.color=color;

this.type=type;

this.time=time;

this.info=info;

}

function info()

{document.writeln("颜色:",this.color);

document.writeln("类型:",this.type);

document.writeln("出厂日期:",this.time);

}

创建Oven类对象实例:

oven1=new Oven("红色","精巧型","99.1.1");

oven2=new Oven("蓝色","典雅型","99.10.1");

oven3=new Oven("黄色","实用型","2000.1.1");

jscript1.html:





"欢迎选购"



function Oven(color,type,time)

{this.color=color;

this.type=type;

this.time=time;

this.info=info;

}

function info()

{document.writeln("颜色:",this.color,"
");

document.writeln("类型:",this.type,"
");

document.writeln("出厂日期:",this.time,"
");

}







烤箱商品










oven1.info();





















Javascript还可通过下列方法方便地创建一个对象且可随意添加属性:

var oven1=new Object();

oven1.color="c1";

oven1.type="t1";

oven1.time="t1";


Javascript的可用对象有:


anchor

applet

button

checkbox

Date

document

form

history

link

location

Math

password

radioButton

reset

selection

string

submit

text

textArea

window


Navigator的对象继承树


navigator

window


history

document


anchors

forms

links

location


一:window对象


window对象是 Javascript的最高层对象,它包含除了Navigator对象外的所有对象。


注:有些方法和属性只有支持Javascript1.2的浏览器才能使用。


1.属性:


defaultStatus:设置状态行默认字符串,只读。


status:描述状态行字符串,可读写。


frames:包含窗口中所有FRAME的对象数组,frames.length可取得FRAME个数。


frame:一个FRAME对象。


self:当前窗口。


parent:父窗口或frameset。


top:当前窗口的最高层窗口。


name:窗口的名字。


innerHeight:给出浏览器窗口内部高度。


innerWidth:给出浏览器窗口内部宽度。


outerHeight:给出整个浏览器窗口高度。


outerWidth:给出整个浏览器窗口宽度。


locationbar:设置显示或隐藏浏览器地址栏,如:locationbar=true


menubar:设置显示或隐藏浏览器菜单栏,如:menubar=false


personalbar:设置显示或隐藏浏览器指示栏,如:personalbar=false


scrollbars:设置显示或隐藏窗口滚动条,如:scrollbars=true


statusbar:设置显示或隐藏浏览器状态栏,如:statusbar=true


toolbar:设置显示或隐藏浏览器工具栏,如:toolbar=false


java:提供访问JAVA API类属性和方法,如:java.lang.Math.random。


netscape:提供引用netscape.*工具包。


sun:提供引用sun.*工具包。


document:窗口中当前所包含的文档对象。


location:指定当前文档URL的字符串。


history:与窗口关联的history对象。


2. 方法


alert("string"):打开一个警告对话框并显示string信息。


confirm("string"):打开一个带有OK和Cancel按钮的对话框并显示string信息,若用户选择OK返回true,选择Cancel返回false。


prompt("string"): 打开一个显示string信息并带有单行文本输入域的对话框,对话框关闭时返回用户输入的字符串。(见窗口举例1)


setTimeout:以毫秒为单位为一将要发生事件设置时间。(见窗口举例2和3)


clearTimeout:重置setTimeout所作的设置。


captureEvent(eventType):设置窗口捕捉指定类型的事件。


releaseEvents(eventType):停止捕捉指定类型事件。


handleEvent(event):若设置了captureEvent(),则指定类型的事件将传递给handleEvent()方法。


focus():使窗口得到输入焦点。


blur():除去当前窗口中的输入焦点。


stop():停止当前文件的下载,相当按下“Stop”按钮。


close():关闭窗口。


open(url,name,features,replaceFlag):查找一窗口或打开一新窗口。(见窗口举例2)


url:指定窗口中显示的资源,空字符串将打开一空白窗口。


name:是窗口的名字。


replaceFlag:值为true时,新窗口将取代原窗口在历史记录中的位置。


features:给出逗号分隔窗口属性值,如:menubar=no。


features:


alwaysLowered=yes|no:窗口是否总在其它窗口下面。


alwaysRaised=yes|no:窗口是否总在其它窗口上方。


dependent=yes|no:窗口是否依附于父窗口,即父窗口关闭时它也关闭。


hotkeys=yes|no:是否取消热键。


location=yes|no:是否显示当前URL。


menubar=yes|no:是否显示菜单栏。


resizable=yes|no:是否允许用户改变窗口大小。


scrollbars=yes|no:是否带滚动条。


status=yes|no:是否显示窗口底部的状态栏。


titlebar=yes|no:是否显示标题栏。


toolbar=yes|no:是否显示工具栏。


z-lock=yes|no:是否固定窗口位置。


height|innerHeight=象素值:窗口内容区高度。


width|innerWidth=象素值:窗口内容区宽度。


outerHeight=象素值:窗口高度。


outerWidth=象素值:窗口宽度。


screenX=象素值:窗口左边框位置。


screenY=象素值:窗口上边框位置。


3.窗口事件


onBlur():窗口失去焦点时。


onDragdrop():当文件或快捷方式被拖动到窗口时。


onError:当出现Javascript错误时。


onFocus():窗口获得焦点时。


onLoad():浏览器装入页面时。


onMove():窗口被移动时。


onResize():窗口被改变大小时。


onUnload():用户离开页面时。


4.窗口举例


例1: promptdemo.html






javascript prompt demo












例2: windowDemo.html








Javascript window Demo





这是窗口大小使用演示,请注意看一下状态栏!









例二: windowdemo2.html




function scroll(seed)

{var msg="欢迎来到袁方JAVA讲坛,讲坛网址:www.triworks.com.cn";

var out = " ";

var c = 1;

if (seed > 100)

{seed--;

var cmd="scroll(" + seed + ")";

timerTwo=window.setTimeout(cmd,100);

}

else

if (seed <= 100 && seed > 0)

{for (c=0 ; c < seed ; c++)

{out+=" ";

}

out+=msg;

seed--;

var cmd="scroll(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,100);

}

else

if (seed <= 0)

{if (-seed < msg.length)

{out+=msg.substring(-seed,msg.length);

seed--;

var cmd="scroll(" + seed + ")";

window.status=out;

timerTwo=window.setTimeout(cmd,100);

}

else

{window.status=" ";

timerTwo=window.setTimeout("scroll(100)",7);

}

}

}

timerONE=window.setTimeout('scroll(100)',50);







Javascript window Demo2










最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 23:30 , Processed in 0.270618 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部