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[编辑]