1、對象屬性檢索
var student = { name:"zhan"};
要檢索name,可以用student["name"] 或者 student.name。
2、對象屬性更新
student.name = 'wang';
student.nickname = 'yu';//對象之前沒有nickname屬性,則自動擴充到改對象中。
3、對象的引用
var x = student;
x.nickname = 'Tom';
var nick = student.nickname;
因為x和student都是指向同一個對象的引用,所以nick為'Tom'。
4、hasOwnProperty 檢查對象是否擁有屬性,它不會檢查原型鏈。
5、用for in 遍曆一個對象中的所有屬性名稱時,改枚舉過程會列出所有屬性,包括函數和原型中的屬性。
var name;
for(name in student){
};
6、4種function調用模式:
方法調用模式:當一個function被儲存為對象的一個方法,就被稱為方法。
var myObject = { value:0; inscrement: function (inc) { this.value += typeof inc ==='number'? inc : 1; }};myObject.increment();document.writeln(myObject.value); //1myObject.increment(2);document.writeln(myObject.value); //3
函數調用模式:當function不是一個對象的屬性時,則被當做一個函數來調用
myObject.double = function () { var that = this; var helper = function () { that.value = add(that.value, that.value); }; helper(); // 以函數的形式調用helper};//以方法的形式調用doublemyObject.double();documnet.writeln(myObject.getValue()); //6
構造器調用模式:
//建立一個名為Quo的構造器函數。它構造一個帶有status屬性的對象。var Quo = function (string) { this.status = string;};//給Quo的所有執行個體提供一個名為get_status的公用方法。Quo.prototype.get_status = function (){ return this.status;};//構造一個Quo執行個體var myQuo = new Quo("confused");document.writeln(myQuo.get_status());
Apply調用模式:apply方法讓我們構建一個參數數組並用其去調用函數。它允許我們選擇this的值。apply方法接受2個參數。第一個是將被綁定給this的值,第二個就是一個參數數組。
//構造一個包含2個數位數組,並將它們相加var array = [3,4];var sum = add.apply(null,array); //sum值為7//構造一個包含status成員的對象。var statusObject = { status:'A-OK'};//statusObject 並沒有繼承自Quo.prototype,但我們可以再statusObject上調用get_status方法,儘管statusObject並沒有一個名為get_status的方法var status = Quo.prototype.get_status.apply(statusObject);//status的值為A-OK
7、給類型添加方法: