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

Javascript: "Top-level 'this' expression" - Stack Overflow

programmeradmin0浏览0评论

I get the following warning/hint/error when I have an object like this:

(
    function global(){...};
    function moreFunctions(){...};
)(this);

"Top-level 'this' expression. This inspection reports instances of Javascript 'this' expression occuring outside of object literals or constructor bodies. Such this expressions are legal Javascript, and reference the top-level "global" Javascript object, but are largely useless." (by InspectionJS)

By the way, jQuery has the same with (window) instead of (this).

I don't understand what it means. All I know is that everthing between the first ( and second ) is an object, but what's that addition?

I've got into this because I've just discovered a JS library source and somehow when it is included in my existing scripts everything just stops working. When I deleted that (this); part it didn't crash the page; but the library just didn't work.

I get the following warning/hint/error when I have an object like this:

(
    function global(){...};
    function moreFunctions(){...};
)(this);

"Top-level 'this' expression. This inspection reports instances of Javascript 'this' expression occuring outside of object literals or constructor bodies. Such this expressions are legal Javascript, and reference the top-level "global" Javascript object, but are largely useless." (by InspectionJS)

By the way, jQuery has the same with (window) instead of (this).

I don't understand what it means. All I know is that everthing between the first ( and second ) is an object, but what's that addition?

I've got into this because I've just discovered a JS library source and somehow when it is included in my existing scripts everything just stops working. When I deleted that (this); part it didn't crash the page; but the library just didn't work.

Share Improve this question edited Nov 7, 2012 at 19:13 Nadav S. asked Nov 7, 2012 at 19:07 Nadav S.Nadav S. 2,4593 gold badges25 silver badges39 bronze badges 3
  • That syntax won't work; to make it more explicit, your two functions should be inside the unnamed function in Neal's answer. – Izkata Commented Nov 7, 2012 at 19:11
  • For more information, see here: stackoverflow./questions/1958383/… – Izkata Commented Nov 7, 2012 at 19:12
  • "All I know is that everthing between the first ( and second ) is an object" -- incorrect. Parentheses don't denote objects. Curly braces do: {} – Ryan Kinal Commented Feb 19, 2013 at 15:34
Add a ment  | 

3 Answers 3

Reset to default 8

I'm not 100% sure on the problem you're having but let me explain what I learned from Paul Irish's video on lessons from the JQuery source.

(function(window, undefined) {
})(this);

This is known as a self-executing function. The function definition is put in brackets. (anything can be put in brackets in Javascript almost all the time). Then, the second () immediately calls the function.

so it's like doing the following

function my_func(window, undefined){...}
my_func(this);

Now to explain the this. Usually, we wrap our entire program in such a self-executing function. The this while calling, and the window and undefined are pretty much just fixes for edge-cases when sharing the code space. For example, someone could put something like

window = 0;
//or
undefined = 1;

suddenly, these very important global variables we rely on break. using this at the top level returns the window variable to the inner function. And since we don't pass any second variable to the function, undefined is back to its correct value.

Hope all this helped.

It's plaining that you're using this outside of any function.

Inside a function, the use of this (generally) means that the programmer intended the function as a method (where this is the object that the method is called on), but outside a function, it's just a strange thing to do.

It's better to write window instead, to explicitly refer to the global object, rather than to rely on the fact that this implicitly refers to the global object when it's not used in a method call.

This puts a whole chunk of code into a immediately executing function with its own scope.

For example:

(function(w){
    var apple = 'apple';
})(window);

alert(apple); //undefined
发布评论

评论列表(0)

  1. 暂无评论