javascript曾一度被认为是玩具型的语言,因为它太容易上手,而且,javascript曾一度担任为web站点“打杂”的职责。直到Ajax的兴起,这个在幕后默默无闻多年的语言才崭露头角,它的灵活性赋予了开发者无穷的想象力。javascript不提供传统的OOP方式,但是仍然可以轻松的实现OOP。
[强大的原型prototype]
这是一段来自《javascript design patterns》的代码:
/* Start and stop animations using functions. */
function startAnimation() {
...
}
function stopAnimation() {
...
}
/* Anim class. */
var Anim = function() {
...
};
Anim.prototype.start = function() {
...
};
Anim.prototype.stop = function() {
...
};
/* Usage. */
var myAnim = new Anim();
myAnim.start();
...
myAnim.stop();
/* Anim class, with a slightly different syntax for declaring methods. */
var Anim = function() {
...
};
Anim.prototype = {
start: function() {
...
阅读下一篇:javascript面向对象编程(二)