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 Passing variable arguments to superclass constructor - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Passing variable arguments to superclass constructor - Stack Overflow

programmeradmin3浏览0评论

What is the best/remended way to pass variable arguments to a superclass constructor? The background explains the problem I'm trying to solve.

Background

I'm porting some code from Java to Javascript. One of the coding patterns that Java has is function overloading. Java picks the best match to determine what function to call. This bees interesting when the function is a Class constructor method.

So code in Java might be

public class MyParser extends Parser {
    public int parse(String str) { super(str); ... } 
    public int parse(String str, int base) { super(str, base); ... }
}

In Javascript bees:

class MyParser extends Parser {
    constructor(){
        super(arguments); // THIS DOES NOT WORK
        if (arguments.length === 1 && typeof arguments[0] === 'string'){
            constructor_1arg(arguments[0]);
        } else if (...){
            constructor_2args(arguments[0], arguments[1]);
        }
    }
}

So I was wondering what is the best way to handle this case?

Possible solution

NOTE: I'm asking this because calling super() is a special case in a constructor and you can only call it once. So I didn't expect this to work but it did (in NodeJS v8.2).

class MyParserTry2 extends Parser {
    constructor(){
        console.log('MyParser2 passed ' + arguments.length + ' args. ', arguments);
        if (arguments.length === 1 && typeof arguments[0] === 'string'){
            super(arguments[0]);
            this.constructor_1arg(arguments[0]);
        } else if (arguments.length === 2 && typeof arguments[0] === 'string'){
            super(arguments[0], arguments[1]);
            this.constructor_2args(arguments[0], arguments[1]);
        }
    }

    constructor_1arg(arg1){ }
    constructor_2args(arg1, arg2){ }
}

I thought super() had to be called before other code, but this might be from another language. Does Javascript standard support calling super() as shown in this code snippet?

Previous answers/research

I searched StackOverflow for answers and found several similar questions but not this one. I found these questions or postings:

  • Passing all arguments forward to another javascript function. Thanks to @BobStein-VisiBone who suggested that this question be re-opened because it is slightly different than the following question. He also suggested searching on javascript pass all callers arguments to inner function
  • Passing an array as a function parameter
  • It was important to realize that arguments is not an array and can't be used to just pass along arguments to an inner function. See Understanding what arguments object really is
  • Using apply() was suggested in some posts, but I didn't see anyone using super.apply(this, arguments). If this is the best way to handle this question suggest this as the answer, but realize that my observations are that "this" is not set until after super() is called. Interesting...
  • What I tried is at JSFiddle. You'll have to open your browsers console to see log messages. Sorry it's not easier to use.

None of the above, however, answer my question.

So... What is the best/remended way to pass variable arguments to a superclass constructor?

What is the best/remended way to pass variable arguments to a superclass constructor? The background explains the problem I'm trying to solve.

Background

I'm porting some code from Java to Javascript. One of the coding patterns that Java has is function overloading. Java picks the best match to determine what function to call. This bees interesting when the function is a Class constructor method.

So code in Java might be

public class MyParser extends Parser {
    public int parse(String str) { super(str); ... } 
    public int parse(String str, int base) { super(str, base); ... }
}

In Javascript bees:

class MyParser extends Parser {
    constructor(){
        super(arguments); // THIS DOES NOT WORK
        if (arguments.length === 1 && typeof arguments[0] === 'string'){
            constructor_1arg(arguments[0]);
        } else if (...){
            constructor_2args(arguments[0], arguments[1]);
        }
    }
}

So I was wondering what is the best way to handle this case?

Possible solution

NOTE: I'm asking this because calling super() is a special case in a constructor and you can only call it once. So I didn't expect this to work but it did (in NodeJS v8.2).

class MyParserTry2 extends Parser {
    constructor(){
        console.log('MyParser2 passed ' + arguments.length + ' args. ', arguments);
        if (arguments.length === 1 && typeof arguments[0] === 'string'){
            super(arguments[0]);
            this.constructor_1arg(arguments[0]);
        } else if (arguments.length === 2 && typeof arguments[0] === 'string'){
            super(arguments[0], arguments[1]);
            this.constructor_2args(arguments[0], arguments[1]);
        }
    }

    constructor_1arg(arg1){ }
    constructor_2args(arg1, arg2){ }
}

I thought super() had to be called before other code, but this might be from another language. Does Javascript standard support calling super() as shown in this code snippet?

Previous answers/research

I searched StackOverflow for answers and found several similar questions but not this one. I found these questions or postings:

  • Passing all arguments forward to another javascript function. Thanks to @BobStein-VisiBone who suggested that this question be re-opened because it is slightly different than the following question. He also suggested searching on javascript pass all callers arguments to inner function
  • Passing an array as a function parameter
  • It was important to realize that arguments is not an array and can't be used to just pass along arguments to an inner function. See Understanding what arguments object really is
  • Using apply() was suggested in some posts, but I didn't see anyone using super.apply(this, arguments). If this is the best way to handle this question suggest this as the answer, but realize that my observations are that "this" is not set until after super() is called. Interesting...
  • What I tried is at JSFiddle. You'll have to open your browsers console to see log messages. Sorry it's not easier to use.

None of the above, however, answer my question.

So... What is the best/remended way to pass variable arguments to a superclass constructor?

Share Improve this question edited Dec 6, 2017 at 17:55 PatS asked Dec 5, 2017 at 17:43 PatSPatS 11.5k17 gold badges69 silver badges121 bronze badges 2
  • 2 stackoverflow./questions/38447168/… but not supported in the referenced Node version? – Dark Falcon Commented Dec 5, 2017 at 17:48
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Rahul R. Commented Dec 5, 2017 at 17:53
Add a ment  | 

1 Answer 1

Reset to default 16
class SomeClass extends SomeSuperClass {
 constructor(...args){ super(...args); }
}

Using arguments is deprecated so you could rather use rest/spread operators.

发布评论

评论列表(0)

  1. 暂无评论