建立Car的數據類型程序 // function car () { } // 建立的共享方法 car . prototype. shift = function () { alert ( this . color +" 汽車 " ) ; } 選擇格式在C++ 代碼中比較常見,格式可相似更新下面如: //建立car 類型 function car () { if (car . prototype shift ) { // if 語句內只能夠執行一次 car . prototype.shift = function () { alert (this . color + " 汽車 " ) ; } } } 相對產生不一樣的相應更換對象以下所示: 產生的函數並進式 用對象function來以函數 car 的類型構成function的自己函數,並且 用prototype屬性能夠擴展和互相替換。 // 建立car 類型 function Car () { this . color = null ;// 能夠省略 this . shift = function () { alert (this . color + " 汽車 " ) ; } } var redCar = new Car () ; redCar . color = " 紅色 " ; var blueCar = new Car () ; blueCar . color = " 藍色 " ; redCar . shift () ; blueCar . shift () ; // 建立 Plane 類型的對象函數式 function plane () { this . shift = function () { alert (this . color + " 飛機 " ) ; } } var redplane = new plane () ; redplane . color = " 紅色 " ; var blueplane = new plane () ; blueplane . color = " 藍色 " ; 構建對象在函數中發生值 redplane . shift () ; blueplane . shift () ; 調用構造函數的執行過程 // 函數值3 function constructor () { this . a = 3 ; } //構造函數值 +a,+b, var obj = new Constructor () ; alert ( " a:" +obj . a+" \ n b:" +obj . b+ " \ n 構造函數:" +obj . constructor); 重寫整個基數的代碼 // O object . prototype . shift = funticon () { alert ( this . color ) ; } var redCar = new object () ; redCar . color = " 紅色 " ; var blueCar = new object () ; blueCar . color = " 藍色 " ; redCar shift () ; blueCar shift () ; 以上代碼擴展了Object構造函數的原型,把shift的式子能夠重複的使用,若是想要建立具備不一樣行爲的shift的式子給另外一個類型的對象如飛機或者汽車就不行了,由於屬性同名了,只在內置構造函數的Prototype只能擴展,不能替換。