哪个 JavaScript 库具有最全面的类继承支持?

发布于 2024-07-16 02:46:17 字数 240 浏览 6 评论 0原文

在使用了十几个不同的 JavaScript 库(例如 Prototype、jQuery、YUI 等)之后,我发现每个不同的库都有不同的方式来模拟某种类层次结构并提供某种类继承支持。 (除了 jQuery 之外)除了非常恼火之外,当您创建一个新类时,它必须依赖于库,除非您采用简单的旧方法。

我想知道哪个库通常为类继承提供最好的支持以及为什么。

我希望有一天 JavaScript 库作者能够就一种类创建和继承风格达成一致。

After playing with a dozen different JavaScript Libraries such as Prototype, jQuery, YUI, just to name a few, I found every different library has a different way of simulating some sort of Class Hierarchy and provide some sort of Class Inheritance support. (Other than jQuery) Other than being very annoyed that when you create a new class, it has to be library dependent, unless you do the plain old way.

I'm wondering which library offers the best support for class inheritance in general and why.

I hope maybe one day JavaScript Library authors can agree on one style for Class creation and inheritance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

情丝乱 2024-07-23 02:46:17

我发现有一个以 Ruby 为模型的 Javascript 框架:

Js.Class

另一个好东西一种是:

Joose-js(以驼鹿为模型( perl) )

我更喜欢Josse,因为它似乎开发得更积极,而且语法看起来也很整洁!

有什么想法吗??? (也许这应该是另一个问题?)

I found out there is a Javascript Framework modeled after Ruby:

Js.Class

Another good one is:

Joose-js (modeled after moose (perl) )

I prefer Josse, because it seems to be more actively developed, and the syntax looks neat too!

Any thoughts??? (Maybe this should be another question??)

掀纱窥君容 2024-07-23 02:46:17

我认为 Microsoft Ajax 完美地实现了它(具有命名空间、继承和功能)。接口等)

示例:

Type.registerNamespace("Demo");

Demo.Person = function(firstName, lastName, emailAddress) {
    this._firstName = firstName;
    this._lastName = lastName;
    this._emailAddress = emailAddress;
}

Demo.Person.prototype = {

    getFirstName: function() {
        return this._firstName;
    },

    getLastName: function() {
        return this._lastName;
    },

    getName: function() {
        return this._firstName + ' ' + this._lastName;
    },

    dispose: function() {
        alert('bye ' + this.getName());
    }
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

I think Microsoft Ajax implements it just perfectly (with namespaces, inheritance & interfaces etc.)

Sample:

Type.registerNamespace("Demo");

Demo.Person = function(firstName, lastName, emailAddress) {
    this._firstName = firstName;
    this._lastName = lastName;
    this._emailAddress = emailAddress;
}

Demo.Person.prototype = {

    getFirstName: function() {
        return this._firstName;
    },

    getLastName: function() {
        return this._lastName;
    },

    getName: function() {
        return this._firstName + ' ' + this._lastName;
    },

    dispose: function() {
        alert('bye ' + this.getName());
    }
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
等往事风中吹 2024-07-23 02:46:17

你应该尝试Classy:

http://classy.pocoo.org/

它很简单,很小,但什么都有我在构建我的课程时需要。

You should try Classy:

http://classy.pocoo.org/

It is simple and really small but has everything I need when constructing my classes.

谎言 2024-07-23 02:46:17

查看原型。 这是一个示例:

// properties are directly passed to `create` method
var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  say: function(message) {
    return this.name + ': ' + message;
  }
});

// when subclassing, specify the class you want to inherit from
var Pirate = Class.create(Person, {
  // redefine the speak method
  say: function($super, message) {
    return $super(message) + ', yarr!';
  }
});

var john = new Pirate('Long John');
john.say('ahoy matey');
// -> "Long John: ahoy matey, yarr!"

Check out Prototype. Here's a sample:

// properties are directly passed to `create` method
var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  say: function(message) {
    return this.name + ': ' + message;
  }
});

// when subclassing, specify the class you want to inherit from
var Pirate = Class.create(Person, {
  // redefine the speak method
  say: function($super, message) {
    return $super(message) + ', yarr!';
  }
});

var john = new Pirate('Long John');
john.say('ahoy matey');
// -> "Long John: ahoy matey, yarr!"
じ违心 2024-07-23 02:46:17

Base2有简单的继承机制,参见John Resig的帖子(帖子中的评论也很有趣)。

还需要记住的是,尝试在 Javascript 中模拟经典的 OO 最近受到了很多批评(尽管在伟大的 JS 库革命的最初几天人们对此进行了非常热情的探索)。

Base2 has simple inheritance mechanism , See John Resig's post on this (comments in the post are also interesting).

Something to also keep in mind is that trying to do simulate classic OO in Javascript has been getting lot of flak lately (although it was explored very enthusiastically in the initial days of great JS library revolution).

素染倾城色 2024-07-23 02:46:17

您可能还对 qooxdoo 感兴趣,它是一个用于创建富互联网应用程序 (RIA) 的框架。 它包括一个全面的 OO 层,旨在强大、优雅和快速:

请参阅手册中的以下部分,其中包含所有详细信息:
http://qooxdoo.org/documentation/0.8#object_orientation

You might also be interested in qooxdoo, a framework for creating rich internet applications (RIAs). It includes a comprehensive OO layer, which aims at being powerful, elegant and fast:

See the following section in the manual, which has all the details:
http://qooxdoo.org/documentation/0.8#object_orientation

丑丑阿 2024-07-23 02:46:17

我发现 mootools 是我继承所需的一切。 它使用许多其他 OO 语言的扩展和实现的基本主题。

一位开发人员在与 jquery 的比较中详细介绍了它:

http://jqueryvsmootools.com

你不也需要整个图书馆。 你可以只下载他们的类模型并忽略其余的(动画、dom 操作等)。

I find mootools to be everything I need for inheritence. It uses the basic motif of Extends and Implements of many other OO languages.

One of the developers goes over it in detail in his comparison to jquery:

http://jqueryvsmootools.com

You don't need the whole library either. You can just download their Class model and ignore the rest (animation, dom manipulation, etc).

靑春怀旧 2024-07-23 02:46:17

只需选择最适合您“口味”的一个即可。 最后,它们都在幕后使用原型继承,因此它们都具有大致相同级别的功能。

如果您想要一个高性能的传统类系统,请使用 GWT 并使用纯 Java 进行编程。

我个人更喜欢Ext的组件系统,但那是因为Ext组件实际上有一个完整的生命周期(初始化、渲染、状态持久性和销毁),这反过来又允许组件插件不会因库的微小更新而中断。

Just pick whichever one "tastes" best to you. In the end, they all use prototypal inheritance behind the scenes, so they all have roughly the same level of functionality.

If you want a high-powered traditional class system, use GWT and program in pure Java.

Personally I prefer Ext's component system, but that's because Ext components actually have a full lifecycle (initialization, rendering, state persistence and destruction), which in turn allows component plugins that don't break with minor updates to the library.

孤凫 2024-07-23 02:46:17

好吧,首先你应该问你是否想要一个尝试模拟经典继承的库,还是一个更适合 JS 原生原型继承的库。

DUI(Digg 用户界面库)是一个 jQuery 插件,它采用 Prototype 的旧模型并以更直观的方式对其进行扩展,允许嵌套的静态和动态类、命名空间、对 dontEnum 位的支持等等。 文档可在 Digg 的 GitHub 上找到。 我想你会发现它非常强大。

披露:我为 Digg 工作并编写了 DUI ;)

Well, first you should ask if you want a library that tries to emulate classical inheritance or one that goes more for JS's native prototypal inheritance.

DUI (the Digg User Interface library) is a jQuery addon that takes Prototype's old model and extends it in a much more intuitive way, allowing for nested static and dynamic classes, namespaces, support for the dontEnum bit and more. Documentation is available on Digg's GitHub. I think you'll find it pretty robust.

Disclosure: I work for Digg and wrote DUI ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文