1.JavaScript 中的所有事物都是對象:字符串、數值、數組、函數...
此外,JavaScript 允許自定義對象。
2.創建直接的實例,這個例子創建了對象的一個新實例,并向其添加了四個屬性:
person=new Object();
person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";
替代語法(使用對象 literals):
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
3.使用對象構造器
本例使用函數來構造對象:
實例
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
創建 JavaScript 對象實例
一旦有了對象構造器,就可以創建新的對象實例,就像這樣:
var myFather=new person("Bill","Gates",56,"blue");
var myMother=new person("Steve","Jobs",48,"green");
4.JavaScript 類
JavaScript 是面向對象的語言,但 JavaScript 不使用類。
在 JavaScript 中,不會創建類,也不會通過類來創建對象(就像在其他面向對象的語言中那樣)。
JavaScript 基于 prototype,而不是基于類的。
5.var y=123e5;// 12300000
var z=123e-5;// 0.00123
6.八進制和十六進制
如果前綴為 0,則 JavaScript 會把數值常量解釋為八進制數,如果前綴為 0 和 "x",則解釋為十六進制數。
實例
var y=0377;
var z=0xFF;
提示:絕不要在數字前面寫零,除非您需要進行八進制轉換。
7.數字屬性和方法
屬性:
MAX VALUE
MIN VALUE
NEGATIVE INFINITIVE
POSITIVE INFINITIVE
NaN
prototype
constructor
方法:
toExponential()
toFixed()
toPrecision()
toString()
valueOf()
8.字符串樣式
document.write("Fontcolor: " + txt.fontcolor("Red") + " <p>")
9.JavaScript String(字符串)對象 實例
(1)document.write(txt.length)計算字符串的長度
(2)document.write("Fontcolor: " + txt.fontcolor("Red") + "<p>")document.write("Fontsize: " + txt.fontsize(16) + "<p>")
document.write("Lowercase: " + txt.toLowerCase() + <p>")document.write("Uppercase: " + txt.toUpperCase() + "<p>")為字符串添加樣式
(3)var str="Hello world!"
document.write(str.indexOf("Hello") + "<p>")
document.write(str.indexOf("World") + "<p>")
document.write(str.indexOf("world"))
輸出0,-1,6indexOf() 方法
(4)var str="Hello world!"document.write(str.match("world") + "<p>")
document.write(str.match("World") + "<p>")
document.write(str.match("worlld") + "<p>")
document.write(str.match("world!"))
輸出world,null,null,world!match() 方法
(5)var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"你好"))如何替換字符串中的字符 - replace()
如何使用 Date() 方法獲得當日的日期。
document.write(Date())
getTime() 返回從 1970 年 1 月 1 日至今的毫秒數。
var d=new Date();
document.write("從 1970/01/01 至今已過去 " + d.getTime() + " 毫秒");
從 1970/01/01 至今已過去 1510461729190 毫秒
var d=new Date()
var weekday=new Array(7)
weekday[0]="星期日"
weekday[1]="星期一"
weekday[2]="星期二"
weekday[3]="星期三"
weekday[4]="星期四"
weekday[5]="星期五"
weekday[6]="星期六"
document.write("今天是" + weekday[d.getDay()])
顯示時鐘
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
var myDate=new Date()myDate.setFullYear(2008,7,9)
9.arr和arr2是兩個數組,合并數組的方法是:
document.write(arr.concat(arr2))
10.document.write(arr.join());用數組的元素組成字符串 - join()
11
12.
13.document.write(Math.random())
使用 random() 來返回 0 到 1 之間的隨機數。
document.write(Math.round(0.60) + "<br>")document.write(Math.round(0.50) + "<br>")
document.write(Math.round(0.49) + "<br>")
document.write(Math.round(-4.40) + "<br>")
document.write(Math.round(-4.60))
如何使用 round()。四舍五入1,1,0,-4,-5
14.RegExp 對象用于規定在文本中檢索的內容。
什么是 RegExp?
RegExp 是正則表達式的縮寫。
當檢索某個文本時,可以使用一種模式來描述要檢索的內容。RegExp 就是這種模式。
簡單的模式可以是一個單獨的字符。
更復雜的模式包括了更多的字符,并可用于解析、格式檢查、替換等等。
可以規定字符串中的檢索位置,以及要檢索的字符類型,等等。
15.
16.RegExp 對象的方法
RegExp 對象有 3 個方法:test()、exec() 以及 compile()。