Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

读了下2.0.0,和自己一直用的Class做了下比较 #15

Open
army8735 opened this issue Mar 8, 2014 · 0 comments
Open

读了下2.0.0,和自己一直用的Class做了下比较 #15

army8735 opened this issue Mar 8, 2014 · 0 comments
Labels

Comments

@army8735
Copy link
Member

army8735 commented Mar 8, 2014

2.0的确精简了很多,也较为易读了。
更新了原有的jsPerf,加了2.0的性能测试,同时添加了我自己的AClass。
http://jsperf.com/class-perfs/11
chrome有时刷不出来图,FF可以,真奇特。

arale的API都知道,不多说,说说我的:

var Animal = AClass(function(name) {
  this.name = name;
}).methods({
  talk: function() {
    return 'I am ' + this.name;
  }
});
var Bird = Animal.extend(function(name) {
  Animal.call(this, name);
}).methods({
  fly: function() {
    return 'I am flying';
  }
});

比较而言:

  • 直接Class(constructor).methods(...)和Class.create({ initialize: constructor, ... })的区别。后者占用了一个initialize非关键字,前者占用了本身的methods方法。不过methods和arale占用的implement又比较像,细节不同。因为传统OOP写法构造函数一般和类名相同,而ES6中是个关键字constructor。我是直接匿名省略,为了凸显构造函数必须首先初始化的概念。
  • extend很类似。arale多了个superClass,继承时Bird.superclass.initialize.call,我是直接Animal.call。感觉加上也容易。
  • methods和implement比较像,arale可传入function因此可添加原型方法。
  • statics在2.0.0去掉了。这个也比较简单。加上会多占用一个,直接在function上写静态的也没啥。去掉少占个变量的确好些。
  • arala创建一个类消耗应该在继承Subclass再继承Class上,我是没有链所以快点。

暂时这些想法,贴下我的源码,可以精简掉static:

define('Class', function() {
    function inheritPrototype(subType, superType) {
        var prototype = Object.create(superType.prototype);
        prototype.constructor = subType;
        subType.prototype = prototype;
        //继承static变量
        Object.keys(superType).forEach(function(k) {
            subType[k] = superType[k];
        });
        return subType;
    }
    function wrap(fn) {
        fn.extend = function(sub) {
            inheritPrototype(sub, fn);
            return wrap(sub);
        }
        fn.methods = function(o) {
            Object.keys(o).forEach(function(k) {
                fn.prototype[k] = o[k];
            });
            return fn;
        };
        fn.statics = function(o) {
            Object.keys(o).forEach(function(k) {
                fn[k] = o[k];
            });
            return fn;
        };
        return fn;
    }
    function klass(cons) {
        return wrap(cons || function() {});
    }
    klass.extend = inheritPrototype;
    return klass;
});

提供之前的思路吧,或许对师兄姐们有所帮助~
最后,期待下ES6的class,然后希望能寻找到一种安全等价无需额外附加的转换方法~目前我想到的要加个superClass才行(因为有super方法)。

class Father() {
  constructor(a) {
    this.a = a;
  }
  method() {
    return this.a;
  }
}
class Child() {
  constructor(a) {
    super(a);
  }
  method() {
    return super.method();
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant