---
title: "选择 JS OOP 实现方式，支持 Aptana IDE 的代码提示"
author: "ncisoft"
date: "2007-09-07T18:51:00Z"
tags: []
categories: ["Web前端"]
slug: "js-oop-aptana-ide"
draft: false
lastmod: "2007-09-07T18:51:00Z"
id: 121755
url: "https://ncisoft.iteye.com/blog/121755"
---

直接写 JS 代码不需要 IDE 支持的牛人可跳过。。。

JS 在实现复杂的 RIA 组件时，如果没有 OOP 的支持会显得很麻烦，不利于代码重用，比如 Ext JS 的 Widget 里面就广泛用到了继承关系：Observable &gt; Button &gt; SplitButton = MenuButton，MooTools 中对接口的使用比较多。这 JS 代码越写越复杂，Aptana 对 JS 的代码提示支持，还是比较能提高生产效率的。

试用过 ExtJS、Prototype、MooTools、Base.js 等支持 OOP 比较好的框架，发现其 OOP 的实现方式都没有考虑到与 Aptana 的兼容性（YUI 代码太大了），就拿 Base 来举例

js 代码

01. var Animal = Base.extend({
02. constructor: function(name) {
03. this.name = name;
04. },
05. \_eat: function() {},
06. \_say: function(message) {}
07. });
08. var Cat = Animal.extend({
09. \_eat: function(food) {}
10. });

折腾半天，也没发现如何让 Aptana 提供代码提示。。。

Ext JS  的 OOP 实现方式比较传统，我喜欢这种风格，可是和 Aptana 的兼容性还是不够好，主要是 Ext.extend(subClass, superClass, overrides) 内部代码提示出不来，加以改造吧。

js 代码

01. /\**
02. * @param {String} name
03. \*/
04. function Aninmal(name) {}
05. Aninmal.prototype =
06. {
07. /\**
08. * eat amount food
09. * @param {Number} amount
10. \*/
11. \_eat: function(amount) {},
12. /\**
13. * @param {String} message
14. \*/
15. \_say: function(message) {}
16. }
17. /\**
18. * @extends {Aninmal} 加上这句，Aptana就知道继承 Aninmal 的属性和方法
19. * @param {Object} name
20. \*/
21. function Cat(name) {}
22. Cat.prototype =
23. {
24. /\**
25. * @param {Number} amount
26. * @param {Boolean} isNotify
27. \*/
28. \_eat: function(amount, isNotify) {}
29. }
30. Ext.Class.extend(Cat, Aninmal, {});  // 伪代码，可以参考 Ext.extend，实现子类对父类的继承
31. var obj = new Cat("");
32. // 对 obj 就可以用上代码提示了