2006-10-11

Prototype-based JavaScript

这几天系统的学习了一下Javascript。以前对Javascript面向对象的特点没什么体会,经过这两天的学习慢慢有了些认识,对于文献中经常提到的Prototype概念也多少有了些理解。这个概念对理解Javascript中的继承还是很重要的。

下面是来自Core JavaScript 1.5 Guide的一个简单例子,可以看看如何继承在Javascript重视如何实现的。

       Employee
       |         |
       |         |
       |         |
Manager     WorkBee
                   |    |
                   |    |
    SalesPerson     Engineer

function Employee () {
this.name = ""
this.dept = "general"
}

function Manager () {
this.reports = [];
}
Manager.prototype = new Employee;

function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;

function SalesPerson () {
this.dept = "sales"
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;

function Engineer () {
this.dept = "engineering"
this.machine = ""
}
Engineer.prototype = new WorkerBee;

没有评论:

发表评论