te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Purpose of Static Methods in ECMAScript 6 Classes - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Purpose of Static Methods in ECMAScript 6 Classes - Stack Overflow

programmeradmin3浏览0评论

What sort of issues with ES5 are static class methods in ES6 supposed to deal with?

The Babel documentation has the following example in its section regarding ES6 classes, though it does not actually state what this pattern acplishes.

Classes support prototype-based inheritance, super calls, instance and static methods and constructors

class SkinnedMesh extends THREE.Mesh {
  constructor(geometry, materials) {
    super(geometry, materials);

    this.idMatrix = SkinnedMesh.defaultMatrix();
    this.bones = [];
    this.boneMatrices = [];
    //...
  }
  update(camera) {
    //...
    super.update();
  }
  static defaultMatrix() {
    return new THREE.Matrix4();
  }
}

What sort of issues with ES5 are static class methods in ES6 supposed to deal with?

The Babel documentation has the following example in its section regarding ES6 classes, though it does not actually state what this pattern acplishes.

Classes support prototype-based inheritance, super calls, instance and static methods and constructors

class SkinnedMesh extends THREE.Mesh {
  constructor(geometry, materials) {
    super(geometry, materials);

    this.idMatrix = SkinnedMesh.defaultMatrix();
    this.bones = [];
    this.boneMatrices = [];
    //...
  }
  update(camera) {
    //...
    super.update();
  }
  static defaultMatrix() {
    return new THREE.Matrix4();
  }
}
Share Improve this question edited Apr 19, 2016 at 19:46 Walter Roman asked Jun 14, 2015 at 5:13 Walter RomanWalter Roman 4,7792 gold badges33 silver badges37 bronze badges 4
  • 5 Personally, I use the static keyword to indicate to the developer that this method doesn't use information from the current instance - similar to using underscore-prefixed names to indicate private properties - even though that may not necessarily be the case, it's a nice bit of self-menting if used correctly – CodingIntrigue Commented Jun 15, 2015 at 11:38
  • Very good example, thanks! – Martin Meeser Commented Apr 14, 2016 at 9:18
  • May I suggest to rename 'defaultMatrix' to 'createDefaultMatrix', because it is a very classical 'Factory Method' pattern (encapsulation of 'new' operator), and many agree to that convention. – Martin Meeser Commented Apr 14, 2016 at 9:29
  • @MartinMeeser As stated in the question, this code was pulled from Babel's documentation. – Walter Roman Commented Apr 19, 2016 at 19:47
Add a ment  | 

2 Answers 2

Reset to default 9

If you pile ES6 code with Babel, and some class contains a static method, ES5-generated code will be just adding that static function to the constructor function.

So, this ES6 ES2015 code:

class A {
   static doStuff() {}
}

...equals (in ES5):

function A() { }
A.doStuff = function() { };

Why you need static functions? Well, transpiled code won't support statics at all, since even functions are objects and static functions are turned into own properties of constructor function.

Static functions or properties can be used to implement factory pattern:

class A {
   static create() {
      // Specific factory method code
   } 
}

var instance = A.create();

Anyway, static member usage is a very diffuse topic and it goes out of scope of an objective answer. It has a lot of use cases and these are universal to any programming language.

Consider class that contains only static methods:

class MyNamespace {
  static foo() { ... }
  static bar() { foo(); }
}

It is quite convenient way of organizing code - put stuff in namespaces.

MyNamespace.foo();
MyNamespace.bar();

That's other than standard static method use cases in other OOP languages.

发布评论

评论列表(0)

  1. 暂无评论