|
構(gòu)造對(duì)象 function MyFunc() {}; // 定義一個(gè)空函數(shù)
var anObj = new MyFunc(); // 使用new操作符,借助MyFun函數(shù),就創(chuàng)建了一個(gè)對(duì)象
function MyFunc(){};
var anObj = {}; // 創(chuàng)建一個(gè)對(duì)象 MyFunc.call(anObj); // 將anObj對(duì)象作為this指針調(diào)用MyFunc函數(shù)
1 function Person(name) // 帶參數(shù)的構(gòu)造函數(shù)
2 { 3 this .name = name; // 將參數(shù)值賦給給this對(duì)象的屬性 4 this .SayHello = function () {alert( " Hello, I'm " + this .name);}; // 給this對(duì)象定義一個(gè)SayHello方法。 5 }; 6 7 function Employee(name, salary) // 子構(gòu)造函數(shù) 8 { 9 Person.call( this , name); // 將this傳給父構(gòu)造函數(shù) 10 this .salary = salary; // 設(shè)置一個(gè)this的salary屬性 11 this .ShowMeTheMoney = function () {alert( this .name + " $ " + this .salary);}; // 添加ShowMeTheMoney方法。 12 }; 13 14 var BillGates = new Person( " Bill Gates " ); // 用Person構(gòu)造函數(shù)創(chuàng)建BillGates對(duì)象 15 var SteveJobs = new Employee( " Steve Jobs " , 1234 ); // 用Empolyee構(gòu)造函數(shù)創(chuàng)建SteveJobs對(duì)象 16 17 BillGates.SayHello(); // 顯示:I'm Bill Gates 18 SteveJobs.SayHello(); // 顯示:I'm Steve Jobs 19 SteveJobs.ShowMeTheMoney(); // 顯示:Steve Jobs $1234 20 21 alert(BillGates.constructor == Person); // 顯示:true 22 alert(SteveJobs.constructor == Employee); // 顯示:true 23 24 alert(BillGates.SayHello == SteveJobs.SayHello); // 顯示:false
function SayHello() // 先定義一份SayHello函數(shù)代碼
{ alert( " Hello, I'm " + this .name); }; function Person(name) // 帶參數(shù)的構(gòu)造函數(shù) { this .name = name; // 將參數(shù)值賦給給this對(duì)象的屬性 this .SayHello = SayHello; // 給this對(duì)象SayHello方法賦值為前面那份SayHello代碼。 }; var BillGates = new Person( " Bill Gates " ); // 創(chuàng)建BillGates對(duì)象 var SteveJobs = new Person( " Steve Jobs " ); // 創(chuàng)建SteveJobs對(duì)象 alert(BillGates.SayHello == SteveJobs.SayHello); // 顯示:true
function Person(name)
{ this .name = name; // 設(shè)置對(duì)象屬性,每個(gè)對(duì)象各自一份屬性數(shù)據(jù) }; Person.prototype.SayHello = function () // 給Person函數(shù)的prototype添加SayHello方法。 { alert( " Hello, I'm " + this .name); } var BillGates = new Person( " Bill Gates " ); // 創(chuàng)建BillGates對(duì)象 var SteveJobs = new Person( " Steve Jobs " ); // 創(chuàng)建SteveJobs對(duì)象 BillGates.SayHello(); // 通過(guò)BillGates對(duì)象直接調(diào)用到SayHello方法 SteveJobs.SayHello(); // 通過(guò)SteveJobs對(duì)象直接調(diào)用到SayHello方法 alert(BillGates.SayHello == SteveJobs.SayHello); // 因?yàn)閮蓚€(gè)對(duì)象是共享prototype的SayHello,所以顯示:true
1 function Person(name) // 基類構(gòu)造函數(shù)
2 { 3 this .name = name; 4 }; 5 6 Person.prototype.SayHello = function () // 給基類構(gòu)造函數(shù)的prototype添加方法 7 { 8 alert( " Hello, I'm " + this .name); 9 }; 10 11 function Employee(name, salary) // 子類構(gòu)造函數(shù) 12 { 13 Person.call( this , name); // 調(diào)用基類構(gòu)造函數(shù) 14 this .salary = salary; 15 }; 16 17 Employee.prototype = new Person(); // 建一個(gè)基類的對(duì)象作為子類原型的原型,這里很有意思 18 19 Employee.prototype.ShowMeTheMoney = function () // 給子類添構(gòu)造函數(shù)的prototype添加方法 20 { 21 alert( this .name + " $ " + this .salary); 22 }; 23 24 var BillGates = new Person( " Bill Gates " ); // 創(chuàng)建基類Person的BillGates對(duì)象 25 var SteveJobs = new Employee( " Steve Jobs " , 1234 ); // 創(chuàng)建子類Employee的SteveJobs對(duì)象 26 27 BillGates.SayHello(); // 通過(guò)對(duì)象直接調(diào)用到prototype的方法 28 SteveJobs.SayHello(); // 通過(guò)子類對(duì)象直接調(diào)用基類prototype的方法,關(guān)注! 29 SteveJobs.ShowMeTheMoney(); // 通過(guò)子類對(duì)象直接調(diào)用子類prototype的方法 30 31 alert(BillGates.SayHello == SteveJobs.SayHello); // 顯示:true,表明prototype的方法是共享的
function Person(name)
{ this .name = name; }; Person.prototype.company = " Microsoft " ; // 原型的屬性 Person.prototype.SayHello = function () // 原型的方法 { alert( " Hello, I'm " + this .name + " of " + this .company); }; var BillGates = new Person( " Bill Gates " ); BillGates.SayHello(); // 由于繼承了原型的東西,規(guī)規(guī)矩矩輸出:Hello, I'm Bill Gates var SteveJobs = new Person( " Steve Jobs " ); SteveJobs.company = " Apple " ; // 設(shè)置自己的company屬性,掩蓋了原型的company屬性 SteveJobs.SayHello = function () // 實(shí)現(xiàn)了自己的SayHello方法,掩蓋了原型的SayHello方法 { alert( " Hi, " + this .name + " like " + this .company + " , ha ha ha " ); }; SteveJobs.SayHello(); // 都是自己覆蓋的屬性和方法,輸出:Hi, Steve Jobs like Apple, ha ha ha BillGates.SayHello(); // SteveJobs的覆蓋沒(méi)有影響原型對(duì)象,BillGates還是按老樣子輸出
function Person(name)
{ this .name = name; }; Person.prototype.SayHello = function () // 建立對(duì)象前定義的方法 { alert( " Hello, I'm " + this .name); }; var BillGates = new Person( " Bill Gates " ); // 建立對(duì)象 BillGates.SayHello(); Person.prototype.Retire = function () // 建立對(duì)象后再動(dòng)態(tài)擴(kuò)展原型的方法 { alert( " Poor " + this .name + " , bye bye! " ); }; BillGates.Retire(); // 動(dòng)態(tài)擴(kuò)展的方法即可被先前建立的對(duì)象立即調(diào)用
原著:李戰(zhàn)(leadzen) http://www.cnblogs.com/leadzen/archive/2008/02/25/1073404.html |
|
|