一、基本概念
1,自定義對象。
根據JS的對象擴充機制,使用者可以自定義JS對象。與自定義對象相對應的是JS标準對象,例如Date、Array、Math等等。
2,原型(prototype)
在JS中,這是一種建立對象屬性和方法的方式,通過prototype可以為對象添加新的屬性和方法。通過prototype我們可以為JS标準對象添加新的屬性和方法,例如對于String對象,我們可以為其添加一個新的方法trim()。我們可以在運作期間為JS對象動态添加新的屬性。
二、文法規則
1、對象建立方式
1)對象初始化器方式
格式:objectName = {property1:value1, property2:value2 , … , propertyN:valueN}
property:對象的屬性
value:對象的值,值可以是字元串、數字或對象三者之一
//例:初始化User對象
var user1 = { name: "user1", age: 18 };
alert(user1.name);
<a href="http://www.cnblogs.com/lidabo/admin/javascript:void(0);">複制代碼</a>
//例,初始化對象
var user = { name: "User1", job: { salary: 3000, title: "programmer"} };
alert(user.job.salary);
//例、初始化User對象
var user2 = { name: "user1", age: 18, getName: function () { return this.name; } };
alert(user2.getName());
後面将以構造函數方式為重點進行講解,包括屬性和方法的定義等等,也針對構造函數的方式進行講解。
2)構造函數方式
編寫一個構造函數,并通過new方式來建立對象,構造函數本可以帶有構造參數
//編寫構造函數
function User(name, age) {
//this.name:表示對象的屬性
this.name = name;
this.age = age;
this.canFly = false;
}
//建立對象
var user1 = new User("aaa", 100);
2、定義對象屬性
1)JS中可以為對象定義三種類型的屬性:私有屬性、執行個體屬性和類屬性。
私有屬性隻能在對象内部使用。
執行個體屬性必須通過對象的執行個體進行引用。
類屬性可以直接通過類名進行引用。(相當于c#靜态屬性或變量)
2)私有屬性定義
私有屬性隻能在構造函數内部定義與使用。
文法格式:var propertyName=value;
//例、私有屬性
function User(age) {
//定義私有屬性,私有屬性隻能在對象内部使用,
var isChild = age < 12;
this.isLittleChild = isChild;
var user = new User(15);
alert(user.isLittleChild);
3)執行個體屬性定義,也存在兩種方式:
prototype方式,文法格式:functionName.prototype.propertyName=value。
this方式,文法格式:this.propertyName=value,注意後面例子中this使用的位置。
上面中文法格式中的value可以是字元創、數字和對象。
//prototype方式
function User() { };
//執行個體屬性
User.prototype.name = "user1";
User.prototype.age = 18;
var user = new User();
alert(user.name);
//this方式
//this方式的執行個體屬性
this.name = name;
this.age = age;
4)類屬性定義
文法格式:functionName.propertyName=value
User.Max_Age = 200;
User.Min_age = 0;
alert(User.Max_Age);
5)對于屬性的定義,除了上面較為正規的方式外,還有一種非常特别的定義方式,文法格式: obj[index]=value
function User(name) {
this.age = 18;
this[1] = "ok"; //以Index方式定義
this[200] = "year";
var user = new User("user1");
alert(user[1] +" "+ user[200]);
注意:通過index方式定義的必須使用index方式來引用,而沒有通過index方式定義的,必須以正常方式引用。
3、定義對象方法
1)JS中可以為對象定義三種類型的方法:私有方法、執行個體方法和類方法:
私有方法隻能在對象内部使用
執行個體方法必須在對象執行個體化後才能使用
類方法可以直接通過類名去使用。(相當于c#靜态方法)
注意:方法的定義不能通過前面所說的index方式進行。
2)定義私有方法
私有方法必須在構造函數體内定義,而且隻能在構造函數體内使用。
文法格式:function methodName( arg1 , … , argN){ }
function User(name) {
//私有方法
function getNameLengh(nameStr) {
return nameStr.length;
this.nameLength = getNameLengh(this.name);
var user = new User("aaron");
alert(user.nameLength);
3)定義執行個體方法
目前也可以使用兩種方式:
prototype方式,在構造函數外使用,文法格式:functionName.prototype.methodName=method;
或者 functionName.prototype.methodName=function(arg1,…,argN){};
this方式,在構造函數内部使用,文法格式:this.methodName=method;
或者 this.methodName=function(arg1 , … ,argN){};
上面的文法格式描述中,method是外部已經存在的一個方法,methodName要定義的對象的方法,意思就是将外部的一個方法直接賦給對象的某個方法。
以function(arg1,…,argN){}的方式定義對象方法是開發人員應該掌握的。
//執行個體方法。
this.getName = getUserName;
this.setName = function (name) { this.name = name; };
function getUserName() {
return this.name;
alert(user.getName());
//this方式
this.getName = function () {
};
this.setName = function (newName) {
this.name = newName
//定義執行個體方法其它的一些例子
User.prototype.getName = function () { return this.name; };
User.prototype.setName = setUserName;
function setUserName(name) {
var user = new User("Aaron");
4)定義類方法
類方法需要在構造函數外面定義,可以直接通過構造函數名對其進行引用。
文法格式:functionName.methodName=method;
或者 functionName.methodName=function(arg1 , … , argN){};
//寫法1
}
User.getMaxAge = getUserMaxAge;
function getUserMaxAge() {
return 200;
alert(User.getMaxAge());
//寫法2
}
User.getMaxAge = function () { return 200; };
4、屬性與方法的引用
1)從可見性上說:
私有屬性與方法,隻能在對象内部引用。
執行個體屬性與方法,可以在任何地方使用,但必須通過對象來引用。
類屬性與方法,可以在任何地方使用,但不能通過對象的執行個體來引用
2)從對象層次上說:
可以進行深層次的引用。幾種方式:
簡單屬性:obj.propertyName
對象屬性:obj.innerObj.propertyName
索引屬性:obj.propertyName[index]
對于更深層次的引用與上面類似。
3)從定義方式上說:
通過index方式定義的屬性,必須通過index方式才能引用。
通過非index方式定義的屬性,必須通過正常的方式才能引用。
另外注意:對象的方法不能通過index方式來定義。
5、屬性與方法的動态增加和删除
1)對于已經執行個體化的對象,我們可以動态增加和删除它的屬性與方法,文法如下(假定對象執行個體為obj):
動态增加對象屬性 obj.newPropertyName=value;
動态增加對象方法 obj.newMethodName=method或者=function(arg1,…,argN){}
動态删除對象屬性 delete obj.propertyName
動态删除對象方法 delete obj.methodName
2)例子:
function User(name){
this.name=name;
this.age=18;
}
var user=new User(“user1”);
user.sister=“susan”;
alert(user.sister);//運作通過
delete user.sister;
alert(user.sister);//報錯:對象不支援該屬性
user.getMotherName=function(){return “mary”;}
alert(user.getMotherName());//運作通過
delete user.getMotherName;
alert(user.getMotherName());//報錯:對象不支援該方法