npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

anima-class

v1.0.0

Published

提供简洁的 OO 实现。

Downloads

11

Readme

Class


Build Status Coverage Status

提供简洁的 OO 实现。


使用说明

create Class.create([parent], [properties])

创建一个新类。参数 parent 是继承的父类,properties 是要混入的实例属性。

来看一个简单的例子(线上演示):

/* pig.js */
define(function(require, exports, module) {
    var Class = require('class');

    var Pig = Class.create({
        initialize: function(name) {
            this.name = name;
        },

        talk: function() {
            alert('我是' + this.name);
        }
    });

    module.exports = Pig;
});

initialize 属性,标明初始化方法,会在构建实例时调用。

使用 create 方法创建的类,拥有 extend 方法,可以继续创建子类:

/* red-pig.js */
define(function(require, exports, module) {
    var Pig = require('./pig');

    var RedPig = Pig.extend({
        initialize: function(name) {
            RedPig.superclass.initialize.call(this, name);
        },

        color: '红色'
    });

    module.exports = RedPig;
});

注意:需要在子类方法中,调用父类中的同名方法时,JavaScript 语言自身并没有提供类似 super 的方式来轻松实现。用 createextend 方法创建类时,可以使用 superclass.methodName 来显式调用父类方法。之所以不提供 super 方法,原因有二:

  1. 实现起来较麻烦。现有类库的实现方案,都不完美。
  2. 在 JavaScript 编程中,调用 super 的需求并不多。简单通过 superclass.methodName 来调用已经能够满足需求,并很灵活、清晰。

properties 参数中,除了支持用 initialize 来标明初始化方法,还可以用 Implements 来标明所创建的类需要从哪些类中混入属性:

/* flyable.js */
define(function(require, exports, module) {
    exports.fly = function() {
        alert('我飞起来了');
    };
});
/* flyable-red-pig.js */
define(function(require, exports, module) {
    var RedPig = require('./red-pig');
    var Flyable = require('./flyable');

    var FlyableRedPig = RedPig.extend({
        Implements: Flyable,

        initialize: function(name) {
            FlyableRedPig.superclass.initialize.call(this, name);
        }
    });

    module.exports = FlyableRedPig;
});

注意Implements 采用首字母大写,是因为小写的 implements 是 JavaScript 保留字。大写也表示其特殊性,与 MooTools 的方式一致。

除了 Implements, 还有一个特殊属性:

  • Extends - 用来指定继承的父类,注意只能有一个父类,不支持多继承。

implement SomeClass.implement(properties)

该方法与 Implements 属性的功能类似。当某个类已存在,需要动态修改时,用 implement 方法更便捷。

/* flyable-red-pig-extension.js */
define(function(require, exports, module) {
    var FlyableRedPig = require('./flyable-red-pig');

    FlyableRedPig.implement({
       swim: function() {
           alert('我还会游泳');
       }
    });
});

这样,我们得到了会说话、会飞、还会游泳的飞天红猪侠:

/* test.js */
define(function(require, exports, module) {
    var FlyableRedPig = require('./flyable-red-pig');
    require('./flyable-red-pig-extension');

    var pig = new FlyableRedPig('飞天红猪侠');
    pig.talk(); // alerts '我是飞天红猪侠'
    pig.fly();  // alerts '我飞起来了'
    pig.swim(); // alerts '我还会游泳'
});

extend SomeClass.extend(properties)

Class.create 创建的类,自动具有 extend 方法,功能与 Class.create 完全一样,只是继承的父类是 SomeClass 自身,前面的例子中已说明,不赘述。

Class Class(fn)

将已经存在的 function 函数转换为 Class 类:

function Animal() {
}
Animal.prototype.talk = function() {};

var Dog = Class(Animal).extend({
    swim: function() {}
});

性能对比

Bitdeli Badge