JavaScript 对象 实例

来源:互联网 发布:网络语9999是什么意思 编辑:程序博客网 时间:2024/05/18 21:12

一、Array对象的一些属性

1.slice
Object.slice(var1,var2);
//表示选取对象Object数组中从Object[var1]到Object[var2-1]之间的元素(包含首尾)

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(0,3);
var x=document.getElementById("demo");
x.innerHTML=citrus;

输出Banana(Object[var1]),Orange,Lemon(Object[var2-1])

2.sort
让sort可以实现降序排列
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a}); //传入一个比较函数
var x=document.getElementById("demo");
x.innerHTML=points;
}

输出100,40,25,10,5,1

3.splice
Object.splice(var1,var2,”element1”,”element2”,…);
//表示在Object[var1]处插入元素element1,element2,…

一些有相反作用的属性
1.删除数组的最后一个元素 - pop() VS 删除数组的第一个元素 - shift()
2.数组的末尾添加新的元素 - push() VS 在数组的开头添加新元素 - unshift()
3.删除数组的最后一个元素 - pop() VS 数组的末尾添加新的元素 - push()
4.删除数组的第一个元素 - shift() VS 在数组的开头添加新元素 - unshift()

一些有类似作用的属性
1.转换数组到字符串 -toString() VS 用数组的元素组成字符串 - join()

二、Math对象的一些属性

1.使用 round() 对数字进行舍入
2.使用 random() 来返回 0 到 1 之间的随机数
3.使用 max() 来返回两个给定的数中的较大的数
4.使用 min() 来返回两个给定的数中的较小的数
5.摄氏度与华氏转换:

<!DOCTYPE html><html><head><script>function convert(degree){if (degree=="C") { F=document.getElementById("c").value * 9 / 5 + 32; document.getElementById("f").value=Math.round(F); }else     { C=(document.getElementById("f").value -32) * 5 / 9; document.getElementById("c").value=Math.round(C); }}</script></head><body><p></p><b>Insert a number into one of the input fields below:</b></p><form><input id="c" name="c" onkeyup="convert('C')"> degrees Celsius<br>equals<br> <input id="f" name="f" onkeyup="convert('F')"> degrees Fahrenheit </form><p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p></body></html>

三、通过对象元素使用for…in语句

var x;var txt="";var person={fname:"Bill",lname:"Gates",age:56}; for (x in person){txt=txt + person[x];}

注意:对象使用花括号和方括号的区别。使用花括号是

var Object={element1:"value1",element1:"value2"...}使用方括号是var Object=["element1","element2","element3",...] 

四、JavaScript Browser 对象 实例

1.Window 对象
弹出一个警告框 (alert)
弹出一个确认框,并提醒访客点击的内容 (confirm)
弹出一个提示框 (prompt)
点击一个按钮时,打开一个新窗口 (open)
确保新的窗口获得焦点 (foucs)
关闭新窗口 (close)
返回新窗口的名字 (winObject.name)
传输一些文本到源(父)窗口 (childwindow.opener)
相对于当前位置移动新窗口 (moveBy)
移动新窗口到指定位置 (moveTo)
打印当前页面 (print)
用像素指定窗口大小 (top.resizeBy)
指定窗口大小 (top.resizeto)
由指定的像素数滚动内容 (scrollBy)
滚动到指定内容处 (scrollTo)

2.Navigator 对象
appCodeName 返回浏览器的代码名
appName 返回浏览器的名称
appVersion 返回浏览器的平台和版本信息
cookieEnabled 返回指明浏览器中是否启用 cookie 的布尔值
platform 返回运行浏览器的操作系统平台
userAgent 返回由客户机发送服务器的user-agent 头部的值

3.Screen 对象
availHeight 返回屏幕的高度(不包括Windows任务栏)
availWidth 返回屏幕的宽度(不包括Windows任务栏)
colorDepth 返回目标设备或缓冲器上的调色板的比特深度
height 返回屏幕的总高度
pixelDepth 返回屏幕的颜色分辨率(每象素的位数)
width 返回屏幕的总宽度

4.History 对象
返回一个url的历史清单 (history.length)
创建一个后退按钮 (history.back)
创建一个前进按钮 (history.forward)
从url的历史清单转到指定的url (history.go(var))

5.Location 对象属性
hash 返回一个URL的锚部分
host 返回一个URL的主机名和端口
hostname 返回URL的主机名
href 返回完整的URL
pathname 返回的URL路径名。
port 返回一个URL服务器使用的端口号
protocol 返回一个URL协议
search 返回一个URL的查询部分

0 0