在Flow中动态添加方法/选择性覆盖原型方法

时间:2017-08-16 16:59:09

标签: javascript flowtype

我有一个像这样的构造函数:

define('MAX_SEGMENT_SIZE', 65535);

function blob_create($data) {
    if (strlen($data) == 0)
        return false;
    $handle = ibase_blob_create();
    $len = strlen($data);
    for ($pos = 0; $pos < $len; $pos += MAX_SEGMENT_SIZE) {
        $buflen = ($pos + MAX_SEGMENT_SIZE > $len) ? ($len - $pos) : MAX_SEGMENT_SIZE;
        $buf = substr($data, $pos, $buflen);
        ibase_blob_add($handle, $buf);
    }
    return ibase_blob_close($handle);
}
$blob = blob_create(file_get_contents('Desert.jpg'));
$query = ibase_query($this->db, "INSERT INTO \"ud_ab\" (FILES) VALUES (?)", $blob) or die(ibase_errmsg());

它使用这样:

function IDBCrud(table: string): void {
   ...
}

IDBCrud.prototype.get = function(...) { ... }
IDBCrud.prototype.post = function(...) { ... }

但有时候,我想直接使用与属性相同的名称来定义对象的方法,以便调用而不是原型的方法。

const accounts = new IDBCrud('Accounts');
accounts.get( ... );
accounts.create( ... );

但是当我跑步时,它失败了:

// Override get method for some reason
accounts.get = function( ... ) {
    // Do some stuffs...
    ...

    // Now call prototype get
    return this.__proto__.get.apply(this, arguments);
}

因为IDBCrud没有&#34;得到&#34;财产(或方法)。但是如果我只是用空值来写它们:

 16: accounts.get = function(match, options) {
                  ^^^ property `get`. Property not found in
 16: accounts.get = function(match, options) {
     ^^^^^^^^^^^^ new object

如果应该在那种情况下工作,但如果这样做,我必须重新定义每一个&#34;得到&#34;调用原型获取方法的方法。

function IDBCrud(...): ... {
    this.get = function() {};
    this.create = function() {};
    ...
}

每次我制作IDBCrud实例时,我都不想这样做,我只想在需要时覆盖它。

没有流量,它不是问题,但有了它,它就会失败。

那么如何通过流程实现这一目标?任何建议都会非常感激。

2 个答案:

答案 0 :(得分:0)

仅在要实现不同行为的对象实例上覆盖它:

//div[@role='rowheader'][.//div[contains(.,'Appleseed, Jonny')]]

答案 1 :(得分:0)

Flow是专为支持es6类而构建的,它阻止我出于安全原因在运行时添加方法。

解决方案很简单,将构造函数转换为类并创建扩展IDBCrud和覆盖方法的新类,它现在正在工作。