js对象知识

来源:互联网 发布:常用cmd 网络命令 编辑:程序博客网 时间:2024/05/16 03:14
JavaScript 提供多个内建对象,比如 String、Date、Array 等等。
JavaScript 只有一种数字类型。所有 JavaScript 数字均为 64 位
JavaScript 中的所有数字都存储为根为 10 的 64 位(8 比特),浮点数。
整数(不使用小数点或指数计数法)最多为 15 位。
小数的最大位数是 17,但是浮点运算并不总是 100% 准确:
如果前缀为 0,则 JavaScript 会把数值常量解释为八进制数,如果前缀为 0 和 "x",则解释为十六进制数。
数字属性和方法
属性:
  • MAX VALUE
  • MIN VALUE
  • NEGATIVE INFINITIVE
  • POSITIVE INFINITIVE
  • NaN
  • prototype
  • constructor
方法:
  • toExponential()
  • toFixed()
  • toPrecision()
  • toString()
  • valueOf()
下面的代码将当前日期与 2008 年 8 月 9 日做了比较:
var myDate=new Date();myDate.setFullYear(2008,8,9);var today = new Date();if (myDate>today){alert("Today is before 9th August 2008");}else{alert("Today is after 9th August 2008");}
Boolean 对象
Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false)。
var myBoolean=new Boolean()
Math 对象
JavaScript 提供 8 种可被 Math 对象访问的算数值:
  • 常数
  • 圆周率
  • 2 的平方根
  • 1/2 的平方根
  • 2 的自然对数
  • 10 的自然对数
  • 以 2 为底的 e 的对数
  • 以 10 为底的 e 的对数
这是在 Javascript 中使用这些值的方法:(与上面的算数值一一对应)
  • Math.E
  • Math.PI
  • Math.SQRT2
  • Math.SQRT1_2
  • Math.LN2
  • Math.LN10
  • Math.LOG2E
  • Math.LOG10E

RegExpRegExp 是正则表达式的缩写对象用于规定在文本中检索的内容。
RegExp 对象有 3 个方法:test()、exec() 以及 compile()。


浏览器对象模型 (BOM)
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:

有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
  • window.innerHeight - 浏览器窗口的内部高度
  • window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
  • document.documentElement.clientHeight
  • document.documentElement.clientWidth
或者
  • document.body.clientHeight
  • document.body.clientWidth
其他 Window 方法
一些其他方法:
  • window.open() - 打开新窗口
  • window.close() - 关闭当前窗口
  • window.moveTo() - 移动当前窗口
  • window.resizeTo() - 调整当前窗口的尺寸
Window Screen
  • screen.availWidth - 可用的屏幕宽度
  • screen.availHeight - 可用的屏幕高度

Window Location
window.location 对象在编写时可不使用 window 这个前缀。
  • location.hostname 返回 web 主机的域名
  • location.pathname 返回当前页面的路径和文件名
  • location.port 返回 web 主机的端口 (80 或 443)
  • location.protocol 返回所使用的 web 协议(http:// 或 https://)
  • location.href 属性返回当前页面的 URL。
Window History
为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。
  • history.back() - 与在浏览器点击后退按钮相同
  • history.forward() - 与在浏览器中点击按钮向前相同
Window Navigator
window.navigator 对象包含有关访问者浏览器的信息。
例:
<div id="example"></div>
<script>
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
document.getElementById("example").innerHTML=txt;
</script>
由于不同的浏览器支持不同的对象,您可以使用对象来检测浏览器。例如,由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。

PopupAlert消息框
JavaScript 中创建三种消息框:警告框alert("文本")、确认框confirm("文本")、提示框prompt("文本","默认值")
提示框:
function disp_prompt()
{
var name=prompt("请输入您的名字","Bill Gates")
if (name!=null && name!="")
{
document.write("你好!" + name + " 今天过得怎么样?")
}
}
确认框:
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
警告框:
function disp_alert()
{
alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
}
计时Timming

setTimeout()
未来的某时执行代码
clearTimeout()
取消setTimeout()

cookie 用来识别用户。
名字 cookie 密码 cookie 日期 cookie
首先,我们会创建一个可在 cookie 变量中存储访问者姓名的函数:
function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}
上面这个函数中的参数存有 cookie 的名称、值以及过期天数。
在上面的函数中,我们首先将天数转换为有效的日期,然后,我们将 cookie 名称、值及其过期日期存入 document.cookie 对象。
之后,我们要创建另一个函数来检查是否已设置 cookie:
function getCookie(c_name){if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } }return ""}
上面的函数首先会检查 document.cookie 对象中是否存有 cookie。假如 document.cookie 对象存有某些 cookie,那么会继续检查我们指定的 cookie 是否已储存。如果找到了我们要的 cookie,就返回值,否则返回空字符串。
最后,我们要创建一个函数,这个函数的作用是:如果 cookie 已设置,则显示欢迎词,否则显示提示框来要求用户输入名字。
function checkCookie(){username=getCookie('username')if (username!=null && username!="") {alert('Welcome again '+username+'!')}else { username=prompt('Please enter your name:',"") if (username!=null && username!="") { setCookie('username',username,365) } }}
例子代码:
<html><head><script type="text/javascript">function getCookie(c_name){if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } }return ""}function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}function checkCookie(){username=getCookie('username')if (username!=null && username!="") {alert('Welcome again '+username+'!')}else { username=prompt('Please enter your name:',"") if (username!=null && username!="") { setCookie('username',username,365) } }}</script></head><body onLoad="checkCookie()"></body></html>

 JavaScript 框架
  • jQuery
  • Prototype
  • MooTools

0 0