創(chuàng)建Car的數(shù)據(jù)類(lèi)型程序
// function car () {
}
// 創(chuàng)建的共享方法
car . prototype. shift = function () {
alert ( this . color +" 汽車(chē) " ) ;
}
選擇格式在C++ 代碼中比較常見(jiàn),格式可類(lèi)似更新下面如:
//創(chuàng)建car 類(lèi)型
function car () {
if (car . prototype shift ) { // if 語(yǔ)句內(nèi)只可以執(zhí)行一次
car . prototype.shift = function () {
alert (this . color + " 汽車(chē) " ) ;
}
}
}
相對(duì)產(chǎn)生不同的相應(yīng)更換對(duì)象如下所示:
產(chǎn)生的函數(shù)并進(jìn)式
用對(duì)象function來(lái)以函數(shù) car 的類(lèi)型構(gòu)成function的本身函數(shù),而且
用prototype屬性可以擴(kuò)展和互相替換。
// 創(chuàng)建car 類(lèi)型
function Car () {
this . color = null ;// 可以省略
this . shift = function () {
alert (this . color + " 汽車(chē) " ) ;
}
}
var redCar = new Car () ;
redCar . color = " 紅色 " ;
var blueCar = new Car () ;
blueCar . color = " 藍(lán)色 " ;
redCar . shift () ;
blueCar . shift () ;
// 創(chuàng)建 Plane 類(lèi)型的對(duì)象函數(shù)式
function plane () {
this . shift = function () {
alert (this . color + " 飛機(jī) " ) ;
}
}
var redplane = new plane () ;
redplane . color = " 紅色 " ;
var blueplane = new plane () ;
blueplane . color = " 藍(lán)色 " ;
構(gòu)建對(duì)象在函數(shù)中發(fā)生值
redplane . shift () ;
blueplane . shift () ;
調(diào)用構(gòu)造函數(shù)的執(zhí)行過(guò)程
// 函數(shù)值3
function constructor () {
this . a = 3 ;
}
//構(gòu)造函數(shù)值 +a,+b,
var obj = new Constructor () ;
alert ( " a:" +obj . a+" \ n b:" +obj . b+ " \ n 構(gòu)造函數(shù):" +obj . constructor);
重寫(xiě)整個(gè)基數(shù)的代碼
// O
object . prototype . shift = funticon () {
alert ( this . color ) ;
}
var redCar = new object () ;
redCar . color = " 紅色 " ;
var blueCar = new object () ;
blueCar . color = " 藍(lán)色 " ;
redCar shift () ;
blueCar shift () ;
以上代碼擴(kuò)展了Object構(gòu)造函數(shù)的原型,把shift的式子可以重復(fù)的使用,如果想要?jiǎng)?chuàng)建具有不同行為的shift的式子給另一個(gè)類(lèi)型的對(duì)象如飛機(jī)或者汽車(chē)就不行了,因?yàn)閷傩酝?,只在?nèi)置構(gòu)造函數(shù)的Prototype只能擴(kuò)展,不能替換。
網(wǎng)站標(biāo)題:以對(duì)象function本身函數(shù)不同的創(chuàng)建car的類(lèi)型數(shù)據(jù)來(lái)重復(fù)或許做更換(開(kāi)發(fā))
當(dāng)前路徑:http://aaarwkj.com/article0/gghsio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站維護(hù)、網(wǎng)站改版、品牌網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站、用戶(hù)體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)