基于JavaScript 類的使用詳解
以下為構(gòu)造函數(shù)方法創(chuàng)建類:
function className (prop_1, prop_2, prop_3) {
this.prop1 = prop_1;
this.prop2 = prop_2;
this.prop3 = prop_3;}
有了上面的類,我們就可以為類創(chuàng)建實(shí)例:
var obj_1 = new className(v1, v2, v3)
var obj_2 = new className(v1, v2, v3)
我們也可以給類添加方法(method),其實(shí)就是Function里的Function。
function className (prop_1, prop_2, prop_3) {
this.prop1 = prop_1;
this.prop2 = prop_2;
this.prop3 = prop_3;
this.func = function new_meth (property) {
//coding here
}
}
屬性訪問域:
在JavaScript里,對(duì)象的屬性默認(rèn)都是全局的,也就是說,對(duì)象內(nèi)外都可以直接訪問該屬性。上面例子里this.prop1, this.prop2, this.prop3都是全局屬性。
如何定義私有屬性呢?使用var,下面的例子里,price就變成了私有屬性!
function Car( listedPrice, color ) {
var price = listedPrice;
this.color = color;
this.honk = function() {
console.log("BEEP BEEP!!");
};
}
如果你想訪問私有屬性,那么你可以在對(duì)象內(nèi)添加一個(gè)方法去返回這個(gè)私有屬性,因?yàn)榉椒ㄔ趯?duì)象內(nèi),所以可以訪問對(duì)象的私有屬性。在外部調(diào)用該方法,就可以訪問到這個(gè)私有屬性了。但是在方法里,就不能再用this.了,像上面的例子,要訪問price,就可以在對(duì)象里添加方法:
this.getPrice = function() {
//return price here!
return price;
--------------------------------------------------------------------------------
繼承:
使用以下語(yǔ)法繼承:
ElectricCar.prototype = new Car();
使用instanceOf檢查對(duì)象是否某對(duì)象的繼承,返回true或false。
myElectricCar instanceof Car
給繼承后的對(duì)象添加方法:
// 使用構(gòu)造函數(shù)定義一個(gè)新的對(duì)象
function ElectricCar( listedPrice ) {
this.electricity=100;
var price = listedPrice;
}
// 使新對(duì)象繼承Car
ElectricCar.prototype = new Car();
// 為新對(duì)象添加方法
ElectricCar.prototype.refuel = function(numHours) {
this.electricity =+ 5*numHours;
};
重寫原型對(duì)象的值或方法:
當(dāng)我們繼承原型對(duì)象后,我們會(huì)繼承原型的值和方法。但有的時(shí)候,我們的對(duì)象值或方法可能會(huì)不同,這時(shí)候,我們可以通過重寫原型對(duì)象的值和方法來改變新對(duì)象的內(nèi)容
function Car( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.numWheels = 4;
this.getPrice = function() {
return price;
};
}
Car.prototype.accelerate = function() {
this.speed += 10;
};
function ElectricCar( listedPrice ) {
var price = listedPrice;
this.electricity = 100;
}
ElectricCar.prototype = new Car();
// 重寫accelerate方法
ElectricCar.prototype.accelerate = function() {
this.speed += 20;
};
// 添加新方法decelerateElectricCar.prototype.decelerate = function(secondsStepped) {
this.speed -= 5*secondsStepped;
};
myElectricCar = new ElectricCar(500);
myElectricCar.accelerate();
console.log("myElectricCar has speed " + myElectricCar.speed);
myElectricCar.decelerate(3);
console.log("myElectricCar has speed " + myElectricCar.speed);
相關(guān)文章
深入理解JavaScript系列(45):代碼復(fù)用模式(避免篇)詳解
這篇文章主要介紹了深入理解JavaScript系列(45):代碼復(fù)用模式(避免篇)詳解,本文講解了默認(rèn)模式、借用構(gòu)造函數(shù)、借用構(gòu)造函數(shù)并設(shè)置原型、共享原型、臨時(shí)構(gòu)造函數(shù)、klass等內(nèi)容,需要的朋友可以參考下2015-03-03
JavaScript bold方法入門實(shí)例(把指定文字顯示為粗體)
這篇文章主要介紹了JavaScript字符串對(duì)象的bold方法入門實(shí)例,bold方法用于把指定文字顯示為粗體,需要的朋友可以參考下2014-10-10
初學(xué)JS的的小例子 javascript replace高亮替換
初學(xué)JS的的小例子 javascript replace高亮替換...2007-12-12
javascript類型系統(tǒng)——undefined和null全面了解
下面小編就為大家?guī)硪黄猨avascript類型系統(tǒng)——undefined和null全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07

