JavaScript/對象

維基教科書,自由的教學讀本

對象[編輯]

在JavaScript中,包括函數、數組在內,除了字面量以外的一切都是對象

創建對象[編輯]

Array對象[編輯]

Function對象[編輯]

Date對象[編輯]

date對象: 用於處理日期和時間,可以通過new關鍵詞來定義date對象,如:

   new MyDate = new Date()

總結[編輯]

關鍵字this[編輯]

關鍵字this允許讀取或改變該對象的屬性。

如果某個函數不是作為對象的方法來調用,那麼函數內this的指向就變得複雜。

例如:

function PopMachineObject()
{
    this.quarters = 0;
    this.dollars = 0;
}
function total_value()
{
    var sum =
    (this.quarters)*25 + (this.dollars)*100;
    return sum;
}
PopMachineObject.prototype.total_value = total_value;
function addquarters( increment ) 
{
    this.quarters+=increment;
}
PopMachineObject.prototype.addquarters = addquarters;
function adddollars( increment )
{
    this.dollars+=increment;
}
PopMachineObject.prototype.adddollars = adddollars;
function test()
{
    pop_machine = new PopMachineObject();
    pop_machine.addquarters(8);
    pop_machine.adddollars(1);
    pop_machine.addquarters(-1);
    alert( "total in the cash register is: " + pop_machine.total_value() );
}

關鍵字globalThis[編輯]