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

oop - Virtual function in Javascript - Stack Overflow

programmeradmin4浏览0评论

I am asking how to implement the virtual function in the javascript like C#, let's say

  • I have a base class A
  • I also have a derived class named B
  • The class A has a function named virtualFunction, this is supposed to be a virtual function like in C#, this can be overridden in the derived class B
  • The function will be called to execute inside the constructor of the base class, A. If the function is overridden in B, B's one will be invoked otherwise A's one will be invoked

With this example below, I want to get a 'B' alert, not the 'A'.

function A() {
  this.virtualFunction();
}

A.prototype.virtualFunction = function() {
  alert('A');
};

//-------------------------------------

function B() {}

B.prototype = new A();
B.prototype.virtualFunction = function() {
  alert('B');
};

var b = new B();

I am asking how to implement the virtual function in the javascript like C#, let's say

  • I have a base class A
  • I also have a derived class named B
  • The class A has a function named virtualFunction, this is supposed to be a virtual function like in C#, this can be overridden in the derived class B
  • The function will be called to execute inside the constructor of the base class, A. If the function is overridden in B, B's one will be invoked otherwise A's one will be invoked

With this example below, I want to get a 'B' alert, not the 'A'.

function A() {
  this.virtualFunction();
}

A.prototype.virtualFunction = function() {
  alert('A');
};

//-------------------------------------

function B() {}

B.prototype = new A();
B.prototype.virtualFunction = function() {
  alert('B');
};

var b = new B();

If we invoke the function after instanced, like this, then it is okay, but I need "inside the constructor"

var b = new B();
b.virtualFunction();

Share Improve this question edited Apr 19, 2016 at 13:21 ManoDestra 6,4736 gold badges27 silver badges50 bronze badges asked Apr 19, 2016 at 13:11 khoailangkhoailang 7441 gold badge18 silver badges39 bronze badges 4
  • 2 In the first case you have alert('A') in both parent and child functions – MysterX Commented Apr 19, 2016 at 13:14
  • 4 Even in C#, a base-class constructor doesn't have access to functions in a derived class. While the base constructor is running, the object is not an instance of the derived class yet, so the derived class's overrides haven not been set up yet. It wouldn't (in general) be safe anyway to call methods in the derived class before the derived constructor has run. – Wyzard Commented Apr 19, 2016 at 13:20
  • Don't use new to create prototype objects! Instead, put a super call in B, and you'll get the expected results. – Bergi Commented Apr 19, 2016 at 13:35
  • @Bergi, thanks, I got your idea – khoailang Commented Apr 19, 2016 at 16:00
Add a ment  | 

1 Answer 1

Reset to default 10

So, the first thing to realize is that JavaScript doesn't use the same semantics as C#. Trying to get it to do so will cause you heartache; you're much better off learning how things are done in JS. In fact, most folks don't try to do a lot of inheritance type stuff in JS, but instead favor position (which is actually a best practice in all OO languages I'm familiar with).

That said, there's a few improvements you could make to your code.

function A() {
  this.virtualFunction();
}

A.prototype.virtualFunction = function() {
  alert('A');
};

//-------------------------------------

function B() {
   A.call(this);
}

B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
B.prototype.virtualFunction = function() {
  alert('B');
};

var b = new B();

发布评论

评论列表(0)

  1. 暂无评论