欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Javascript 面向对象编程定义对象函数的方法

程序员文章站 2022-04-04 21:51:25
...


//A.利用传统的原始方法定义对象的方法(函数) 一

function Programmer(name,age,position){
this.name = name;
this.age = age;
this.position = position;

this.displayInfo = function(){
alert("[Name: "+ this.name +"] [Age: "+this.age+"] [position: "+this.position+"]");
}

this.setName = function(name){
this.name = name;
}

this.setAge = function(age){
this.age = age;
}

this.setPosition = function(position){
this.position = position;
}

}

var oBpmProgrammer = new Programmer("Kevin",24,"BPM Programmer");
oBpmProgrammer.displayInfo(); //[Name: Kevin] [Age: 24] [position: BPM Programmer]
oBpmProgrammer.setName("Andy");
oBpmProgrammer.displayInfo(); //[Name: Andy] [Age: 24] [position: BPM Programmer]


//B.利用传统的原始方法定义对象的方法(函数) 二

function Programmer(name,age,position){
this.name = name;
this.age = age;
this.position = position;
}


Programmer.prototype.displayInfo = function(){
alert("[Name: "+ this.name +"] [Age: "+this.age+"] [position: "+this.position+"]");
}

Programmer.prototype.setName = function(name){
this.name = name;
}

Programmer.prototype.setAge = function(age){
this.age = age;
}

Programmer.prototype.setPosition = function(position){
this.position = position;
}


var oBpmProgrammer = new Programmer("Kevin",24,"BPM Programmer");
oBpmProgrammer.displayInfo(); //[Name: Kevin] [Age: 24] [position: BPM Programmer]
oBpmProgrammer.setName("Andy");
oBpmProgrammer.displayInfo(); //[Name: Andy] [Age: 24] [position: BPM Programmer]


//C.利用原型定义对象方法(函数)的另一种方法

function Programmer(name,age,position){
this.name = name;
this.age = age;
this.position = position;
}

Programmer.prototype = {
displayInfo : function(){
alert("[Name: "+ this.name +"] [Age: "+this.age+"] [position: "+this.position+"]");
},

setName : function(name){
this.name = name;
},

setAge : function(age){
this.age = age;
},

setPosition : function(position){
this.position = position;
},

sex:"male"
}

var oBpmProgrammer = new Programmer("Kevin",24,"BPM Programmer");
oBpmProgrammer.displayInfo(); //[Name: Kevin] [Age: 24] [position: BPM Programmer]
oBpmProgrammer.setName("Andy");
oBpmProgrammer.displayInfo(); //[Name: Andy] [Age: 24] [position: BPM Programmer]
alert(oBpmProgrammer.sex) //male


//D.测试这种方法使用的继承是否有效


function Person(name,age,position){
this.name = name;
this.age = age;
this.position = position;
}

Person.prototype = {
displayInfo : function(){
alert("[Name: "+ this.name +"] [Age: "+this.age+"] [position: "+this.position+"]");
},

setName : function(name){
this.name = name;
},

setAge : function(age){
this.age = age;
},

setPosition : function(position){
this.position = position;
},
sex:"male"
}


function Programmer(name,age,position){
Person.call(this,name,age,position)
}

Programmer.prototype = new Person();

var oBpmProgrammer = new Programmer("Kevin",24,"BPM Programmer");
oBpmProgrammer.displayInfo(); //[Name: Kevin] [Age: 24] [position: BPM Programmer]
oBpmProgrammer.setName("Andy");
oBpmProgrammer.displayInfo(); //[Name: Andy] [Age: 24] [position: BPM Programmer]
alert(oBpmProgrammer.sex) //male