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

Javascript: function.prototype.method - Stack Overflow

programmeradmin1浏览0评论

I guess most of you have seen the following code snippet:

Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};

I also know that it will affect all functions since they are all objects created by Function so that they can access method named "method", however I am confused why Function itself also can also access "method" like following:

Function.method('test', function () {return 1;});

I guess most of you have seen the following code snippet:

Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};

I also know that it will affect all functions since they are all objects created by Function so that they can access method named "method", however I am confused why Function itself also can also access "method" like following:

Function.method('test', function () {return 1;});
Share Improve this question asked Jul 3, 2013 at 11:07 sunqiang.leosunqiang.leo 2353 silver badges8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

Edorka's answer is correct: Function is its own constructor (i.e. "parent").

Function.constructor;  // function Function() { [native code] }

Normally you can't do what you're doing. For example, this won't work:

f = function () {};
f.prototype.a = 5;
f.a;  // undefined

This kind of thing only works if you use a function as a constructor, like so:

f = function () {};
f.prototype.a = 5;
g = new f();
g.a;  // 5

But Function is weird, it is the constructor for all functions and is also a function itself, so it templates its properties off its own prototype. Hence you can call Function.method() in your code.

Because Function is itself a function:

typeof Function === 'function'
Object.getPrototypeOf(Function) === Function.prototype

And you can see it being called as a function (a form of indirect eval):

Function('return 1+2')() === 3

All that as defined in the spec.

zerkms asked in a ment above:

Which came first - the Function object or the Function prototype?

We have to understand that what's exposed to us, the puny programmers, is different than what's represented internally. This can be exemplified by overriding the Array constructor (tip: don't try this while writing an answer, you'll get a lot of errors):

new Array(0, 1, 2); //gives you [0, 1, 2]
Array = function () { return [4] };
new Array(0, 1, 2); //gives you [4]
//however,
[0, 1, 2] //will always give you [0, 1, 2]

This is because of a section in the spec (a bit down, in the "semantics" section):

Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.

Using the array literal (or array initializer as the spec calls it) you ensure that you use the built-in Array constructor.

Why did I give this example? First of all, because it's a fun example. Second, to demonstrate how what we do and what's actually done are different. To answer zerkms, the Function object most likely came first, but that was not the first function. We don't have access to that built-in function.

because new functions are using the Function's prototype, Functions method is using his own prototype methods too.

If you modify one of this methods or attributes and it belonged to a "parent" prototype all the other objects using this prototype will be affected.

Some literacy related to this strange subject: http://www.packtpub./article/using-prototype-property-in-javascript

Consider the following constructor function object:

var Construct = function () { };

And a prototype shared function:

Construct.prototype.hello = function (name) { console.log("Hello " + name); };

Now if you create a new object from the constructor, this gets the shared member function:

var c = new Construct();
c.hello("World");

The same as c is instanceof Construct Object, also any

  • function is instanceof Function and instanceof Object,
  • Function itself is instanceof Function and Object,
  • Construct is instanceof Function Object and also
  • Object is instanceof Function Object.

Every function statement and operator is a literal for a native new Function.
Every { } literal is a native new Object.

Objects created new by a constructor get the members of the constructor.prototype.
Objects can have any member for themselves, only members of prototypes get shared.

发布评论

评论列表(0)

  1. 暂无评论