面向对象的JavaScript工具

Imr*_*hsh 5 javascript architecture jquery mootools prototypejs

我一直在研究我的网站的用户界面(www.swalif.com:如果你愿意,可以使用chrome进行翻译).不熟悉jQuery我开始使用JavaScript,现在文件很大:大约1000行代码.此外,代码处理和更改变得越来越复杂.

因此,我一直在寻找一种能够以面向对象的方式解决这个问题的方法,这种方式可以产生一个具有良好架构的干净,可重复使用的系统.也可以使用JQuery提供的功能来保持代码较小.

问题是那里有很多工具,我无法决定投入时间来完成这项任务.例如mootools,prototype,jQuery等.所以我需要有人带领我朝着正确的方向前进.

这是我们的网站www.swalif.com.任何其他建议也欢迎.

Jak*_*cki 3

对于面向对象的 javascript 开发,我会推荐 John Resig 的Simple javascript Inheritance。它是一小段 JavaScript,允许您定义类、从基类派生并重写方法。

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});
var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  }
});

var p = new Person(true);
p.dance(); // => true

var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true

// Should all be true
p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class
Run Code Online (Sandbox Code Playgroud)