1.對象
對象是 JavaScript 的一種復(fù)合數(shù)據(jù)類型,它可以把多個(gè)數(shù)據(jù)集中在一個(gè)變量中,并且給其中的每個(gè)數(shù)據(jù)起名字。
或者說,對象是一個(gè)屬性的集合,每個(gè)屬性有自己的名字和值。JavaScript 并不像其他 OOP 語言那樣有類的概念,不是先設(shè)計(jì)類再制造對象。(先有對象再加入屬性,可以用一些函數(shù)制造對象)。
var o = new Object() ;
var ciclr = { x:0, y:0, radius:2 };
2.訪問對象屬性
. 運(yùn)算符
var book = new Object();
book.title = " HTML 與秘籍 "; // 只要給 book.title 賦一個(gè)值,book 里就有了 title。
book.translator = "李松峰"; // 可以不斷地動態(tài)往對象里加屬性。
book.chapter1 = new Object();
book.chapter1.title = "HTML5 簡介";
即使構(gòu)造的時(shí)候不存在的屬性也可以在今后隨時(shí)添加。
3.刪除對象屬性
delete book.chapter1;
book.chapter1=null;
4.遍歷所有屬性
for ( var x in o )...
特殊形式的 for 循環(huán)。對于 o 這個(gè) Object 里面每一個(gè)屬性都取出來,循環(huán)的每一遍把它作為 x 拿來用,就可以對 x 做事情。
alert(x) 看到的是屬性的名字
alert(o[x]) 輸出屬性的值
對于一個(gè)對象,同樣可以像數(shù)組一樣訪問里面的屬性,你給他的索引不是數(shù)字而是名字。不止通過?“.”?運(yùn)算符訪問屬性,可以通過方括號,給方括號里一個(gè)名字訪問屬性的值。o[x] 說明對象可計(jì)算,程序可以很靈活。
5.構(gòu)造方法
構(gòu)造函數(shù)
function Rect(w,h){
this.width = w; this.height = h;
this.area = function(){ return this.width*this.height ;};
}
var r = new Rect(5,10); alert(r.area());
不直接制造對象;通過 this 來定義成員;沒有 return。
new 的時(shí)候制造了 this ,交給 Rect 函數(shù)。在 Rect 里面,new 制造出來的對象就叫做 this。
一旦定義了構(gòu)造函數(shù)就可以構(gòu)造任意數(shù)量的對象??梢孕薷膶ο罄锏闹?,繼續(xù)調(diào)用它的函數(shù)算出修改以后值的結(jié)果。
6.原型對象
對象的 prototype 屬性指定了它的原型對象,可以用“.”運(yùn)算符直接讀它的原型對象的屬性。
當(dāng)寫這個(gè)屬性時(shí)才在它自己內(nèi)部產(chǎn)生實(shí)際的屬性。
如果你在你的對象里放了 prototype。prototype可以再有很多的屬性,那些屬性可以直接被當(dāng)作這個(gè)對象的屬性。
function Person(){}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){alert(this.name);};
var person1 = new Person();
person1.sayName(); // Nicholas
var person2 = new Person();
person2.sayName(); // Nicholas
alert(person1.sayName == person2.sayName); // true
person1.name="Greg";
alert(person1.name); // Greg
alert(person2.name); // Nicholas
person1 和 person2 最開始時(shí)都指向 Person 的 prototype。沒有自己的成員(屬性),訪問 person1 任何屬性都從 Person 的 prototype 中來。一旦做了賦值,就擁有了自己的屬性。其他屬性仍然會從 Person 的 prototype 中來。若 delete person1.name 所有屬性又從 Person 的 prototype 里來了。
7.原型的問題
function Person(){}
Person.prototype = {
? ? ? ? constructor : Person,
? ? ? ? name : "Nicholas",
? ? ? ? age:29,
? ? ? ? job:"Software Engineer",
? ? ? ? friends:["Shelby","Court"],
sayName:function(){alert(this.name);}
};
var person1 = new Person();
var person2 = new Person();
person1.friends.push("Van");
alert(person1.friends); // Shelby,Court,Van
alert(person2.friends); // Shelby,Court,Van
在這段程序中 person1.friends 沒有做賦值,還是原來的 friends 。讓它做了一些動作,改變了它自己。沒有讓它等于另外一個(gè)數(shù)組。
做賦值才能讓對象里面有自己的屬性取代 prototype 里的屬性。
8.組合原型和構(gòu)造方法
function Person(name,age,job){
this.name = "Nicholas";this.age = 29;
this.job = "Software Engineer";this.friends = ["Shelby","Court"];
}
Person.prototype={
constructor : Person,
sayName:function(){alert(this.name);}
} // 讓 Person 有自己的東西,this出來的東西都是對象自己的,不共享。
var person1 = new Person("Nicholas",29,"Software Engineer");
var person2 = new Person("Greg",27,"Doctor");
person1.friends.push("Van");
alert(person1.friends); // Shelby,Court,Van
alert(person2.friends); // Shelby,Court
alert(person1.friends===person2.friends); // false === 表示恒等
alert(person1.sayName===person2.sayName); // true?