构造对象 function MyFunc() {}; // 定义一个空函数
var anObj = new MyFunc(); // 使用new操作符,借助MyFun函数,就创建了一个对象
function MyFunc(){};
var anObj = {}; // 创建一个对象 MyFunc.call(anObj); // 将anObj对象作为this指针调用MyFunc函数
1 function Person(name) // 带参数的构造函数
2 { 3 this .name = name; // 将参数值赋给给this对象的属性 4 this .SayHello = function () {alert( " Hello, I'm " + this .name);}; // 给this对象定义一个SayHello方法。 5 }; 6 7 function Employee(name, salary) // 子构造函数 8 { 9 Person.call( this , name); // 将this传给父构造函数 10 this .salary = salary; // 设置一个this的salary属性 11 this .ShowMeTheMoney = function () {alert( this .name + " $ " + this .salary);}; // 添加ShowMeTheMoney方法。 12 }; 13 14 var BillGates = new Person( " Bill Gates " ); // 用Person构造函数创建BillGates对象 15 var SteveJobs = new Employee( " Steve Jobs " , 1234 ); // 用Empolyee构造函数创建SteveJobs对象 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函数代码
{ alert( " Hello, I'm " + this .name); }; function Person(name) // 带参数的构造函数 { this .name = name; // 将参数值赋给给this对象的属性 this .SayHello = SayHello; // 给this对象SayHello方法赋值为前面那份SayHello代码。 }; var BillGates = new Person( " Bill Gates " ); // 创建BillGates对象 var SteveJobs = new Person( " Steve Jobs " ); // 创建SteveJobs对象 alert(BillGates.SayHello == SteveJobs.SayHello); // 显示:true
function Person(name)
{ this .name = name; // 设置对象属性,每个对象各自一份属性数据 }; Person.prototype.SayHello = function () // 给Person函数的prototype添加SayHello方法。 { alert( " Hello, I'm " + this .name); } var BillGates = new Person( " Bill Gates " ); // 创建BillGates对象 var SteveJobs = new Person( " Steve Jobs " ); // 创建SteveJobs对象 BillGates.SayHello(); // 通过BillGates对象直接调用到SayHello方法 SteveJobs.SayHello(); // 通过SteveJobs对象直接调用到SayHello方法 alert(BillGates.SayHello == SteveJobs.SayHello); // 因为两个对象是共享prototype的SayHello,所以显示:true
1 function Person(name) // 基类构造函数
2 { 3 this .name = name; 4 }; 5 6 Person.prototype.SayHello = function () // 给基类构造函数的prototype添加方法 7 { 8 alert( " Hello, I'm " + this .name); 9 }; 10 11 function Employee(name, salary) // 子类构造函数 12 { 13 Person.call( this , name); // 调用基类构造函数 14 this .salary = salary; 15 }; 16 17 Employee.prototype = new Person(); // 建一个基类的对象作为子类原型的原型,这里很有意思 18 19 Employee.prototype.ShowMeTheMoney = function () // 给子类添构造函数的prototype添加方法 20 { 21 alert( this .name + " $ " + this .salary); 22 }; 23 24 var BillGates = new Person( " Bill Gates " ); // 创建基类Person的BillGates对象 25 var SteveJobs = new Employee( " Steve Jobs " , 1234 ); // 创建子类Employee的SteveJobs对象 26 27 BillGates.SayHello(); // 通过对象直接调用到prototype的方法 28 SteveJobs.SayHello(); // 通过子类对象直接调用基类prototype的方法,关注! 29 SteveJobs.ShowMeTheMoney(); // 通过子类对象直接调用子类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(); // 由于继承了原型的东西,规规矩矩输出:Hello, I'm Bill Gates var SteveJobs = new Person( " Steve Jobs " ); SteveJobs.company = " Apple " ; // 设置自己的company属性,掩盖了原型的company属性 SteveJobs.SayHello = function () // 实现了自己的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的覆盖没有影响原型对象,BillGates还是按老样子输出
function Person(name)
{ this .name = name; }; Person.prototype.SayHello = function () // 建立对象前定义的方法 { alert( " Hello, I'm " + this .name); }; var BillGates = new Person( " Bill Gates " ); // 建立对象 BillGates.SayHello(); Person.prototype.Retire = function () // 建立对象后再动态扩展原型的方法 { alert( " Poor " + this .name + " , bye bye! " ); }; BillGates.Retire(); // 动态扩展的方法即可被先前建立的对象立即调用
原著:李战(leadzen) http://www.cnblogs.com/leadzen/archive/2008/02/25/1073404.html |
|