JavaScript OOP

1. 什么是JavaScript
JavaScript 是一种描述性的脚本语言(Script Language),它可以非常自由地被嵌入到HTML 的文件之中。使用JavaScript 可以做什么呢?它的作用很简单,就是对网页浏览者当前所触发的事件进行处理或对网页进行初始化工作。它是事先在网页中编写好代码(或叫做“脚本”),然后 此代码伴随Html文件一起传送到客户端的浏览器上,由浏览器对这些代码进行解释执行,而其执行期间并没有劳驾服务器帮忙,这样就减轻了服务器的负担。
许多编程高手都瞧不起JavaScript编程,认为只是小孩子的玩意。的确,JavaScript作为一种脚本语言,比起其他语言来说,显得非常简单,没有很复杂的语法,没有庞杂的体系架构。
2. 面向对象编程(Object-Oriented Programming)
一个世界,可以完全由对象组成,而将算法所基于的世界只用对象表现出来,再进行后续代码的编写,这种编程方法就被称作面向对象的编程思想。
OOP的三大要素:
1. 封装
2. 继承
3. 多态
3. JavaScript如何实现OOP
3.1. 封装(Wrap)
JavaScript的对象封装,主要依靠function来实现。以下是一个简单的示例:
//*********************************************

// 定义Pet(宠物)对象
//*********************************************
function Pet() {
//名称
this.name = null;
//颜色
this.color = null;
//获取名称
this.getName = function() {
return this.name;
};
//设置名称
this.setName = function(newName) {
this.name = newName;
};
//获取颜色
this.getColor = function() {
return this.color;
};
//设置颜色
this.setColor = function(newColor) {
this.color = newColor;
};
//定义一个需要实现的方法
this.getFood = null;
//获取宠物的描述信息
this.toString = function() {
return "The pet is " + this.name +",it's "+this.color+",and it likes "+this.getFood()+".";
};
}
3.2. 继承(inheritance)
JavaScript的继承的实现主要依靠prototype(原型)来实现,下面为Pet类编写一个子类。
//*********************************************

// 定义Cat(猫)对象
//*********************************************
function Cat() {
//实现Pet中定义的getFood方法
this.getFood = function() {
return "fish";
};
}

//声明Cat的原型,即Cat的父类
Cat.prototype = new Pet;

多层次继承
//*********************************************
// 定义PersianCat(波斯猫)对象
//*********************************************
function PersianCat() {
}

//声明PersianCat的原型,即PersianCat的父类
PersianCat.prototype = new Cat;
3.3. 重载(override)与多态(Polymorphism)
//重载Pet的toString方法
PersianCat.prototype.toString = function() {
return "It's just a persian cat.";
};

注意:Override和Overload的区别,JavaScript是不支持Overload的,原因是……你自己想想吧。

JavaScript是一种弱类型的语言,所以不要把它与C++、JAVA等语言进行比较。
4. 附录
4.1. 完整的示例
4.1.1. Pet.js
//*********************************************

// 定义Pet(宠物)对象
//*********************************************
function Pet() {
//名称
this.name = null;
//颜色
this.color = null;
//获取名称
this.getName = function() {
return this.name;
};
//设置名称
this.setName = function(newName) {
this.name = newName;
};
//获取颜色
this.getColor = function() {
return this.color;
};
//设置颜色
this.setColor = function(newColor) {
this.color = newColor;
};
//定义一个需要实现的方法
this.getFood = null;
//获取宠物的描述信息
this.toString = function() {
return "The pet is " + this.name +",it's "+this.color+",and it likes "+this.getFood()+".";
};
}

//*********************************************
// 定义Cat(猫)对象
//*********************************************
function Cat() {
//实现Pet中定义的getFood方法
this.getFood = function() {
return "fish";
};
}

//声明Cat的原型,即Cat的父类
Cat.prototype = new Pet;

//*********************************************
// 定义PersianCat(波斯猫)对象
//*********************************************
function PersianCat() {
}

//声明PersianCat的原型,即PersianCat的父类
PersianCat.prototype = new Cat;

//重载Pet的toString方法
PersianCat.prototype.toString = function() {
return "It's just a persian cat.";
};

//*********************************************
// 定义Dog(狗)对象
//*********************************************
function Dog() {
//实现Pet中定义的getFood方法
this.getFood = function() {
return "bone";
};
}

//声明Dog的原型,即Dog的父类
Dog.prototype = new Pet;
4.1.2. Pet.htm


4.1.3. 运行结果

弹出三个对话框,分别如下:
The pet is MiMi,it's white,and it likes fish.
The pet is WangWang,it's yellow,and it likes bone.
It's just a persian cat.
本示例在IE6.0下测试通过。

6 条评论:

匿名 说...

This is a topic that's close to my heart... Best wishes! Where are your contact details though?
Feel free to visit my page : epl transfer news 2010 2011

匿名 说...

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a
comment is added I get three e-mails with the same comment.
Is there any way you can remove people from that service?

Thank you!
Also visit my page :: football transfer rumours latest

匿名 说...

Attractive section of content. I just stumbled upon your website and in
accession capital to assert that I acquire actually enjoyed account your blog posts.
Any way I'll be subscribing to your feeds and even I achievement you access consistently rapidly.
Also visit my web site :: comprar perfume

匿名 说...

We are a group of volunteers and opening a new scheme
in our community. Your site provided us with valuable information
to work on. You've done an impressive job and our entire community will be thankful to you.
My webpage :: pizza games lafayette

匿名 说...

Aw, this was a very good post. Finding the time and actual effort to produce a top notch article… but what
can I say… I put things off a whole lot and never seem to get nearly anything done.


Look into my blog :: buy rap beats exclusive
my page: buy rap beats instrumentals

匿名 说...

Hi, Neat post. There is an issue along with your web site in web explorer, would test this?
IE still is the marketplace leader and a large section of other people will omit your great writing because of this problem.


my web site; Buy Likes 365