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
上一篇: JavaScript变量的定义及类型
下一篇: Linux常用命令
推荐阅读
-
php面向对象中static静态属性和静态方法的调用
-
探讨javascript是不是面向对象的语言_javascript技巧
-
Python 类、对象、方法、面向对象的特征
-
ruby 新建对象_Ruby面向对象编程的简介
-
python面向对象编程思维,面向对象概念,类对象,类的方法,继承,方法重写 笔记
-
Java面向对象(1)面向对象的思想概述以及类的介绍,封装和构造方法
-
Python学习---面向对象---类的定义、创建及对象方法的调用
-
python学习笔记之面向对象中的静态方法、类方法、属性方法总结
-
Python学习之面向对象(类的特殊方法)
-
javascript 的面向对象特性参考_基础知识