admin管理员组文章数量:1335832
I have found this code snippet:
;
100% function($) { // WTF?
var _true_ = true; // WTF?
var _false_ = false; // WTF?
var go = function(location, date) {
location || (location = {});
var result = _false_;
if (date && date.day) {
result = geoService.go(location, date);
}
return !!result;
}
var process = function(func) {
var args = [].prototype.slice.call(arguments, 1);
return function() {
return func.apply(this, args);
}
}
// ...
}(jQuery, undefined);
In here: (sorry, no id-s have been found on the page)
I don't understand what these parts are doing:
- the "100%" in the second line
- the
var _true_ = true;
andvar _false_ = false;
assignments in the 3-4 lines
I'm curious, what is the purpose of these.
I have found this code snippet:
;
100% function($) { // WTF?
var _true_ = true; // WTF?
var _false_ = false; // WTF?
var go = function(location, date) {
location || (location = {});
var result = _false_;
if (date && date.day) {
result = geoService.go(location, date);
}
return !!result;
}
var process = function(func) {
var args = [].prototype.slice.call(arguments, 1);
return function() {
return func.apply(this, args);
}
}
// ...
}(jQuery, undefined);
In here: http://www.dofactory./products/javascript-jquery-design-pattern-framework (sorry, no id-s have been found on the page)
I don't understand what these parts are doing:
- the "100%" in the second line
- the
var _true_ = true;
andvar _false_ = false;
assignments in the 3-4 lines
I'm curious, what is the purpose of these.
Share Improve this question edited Jan 25, 2015 at 13:19 Quentin 945k132 gold badges1.3k silver badges1.4k bronze badges asked Jan 25, 2015 at 13:07 HerbertuszHerbertusz 1,2511 gold badge13 silver badges20 bronze badges 02 Answers
Reset to default 9the "100%" in the second line
It's the number 100
followed by a modulus operator. It's not used for anything (since the result isn't captured) other than to force the right hand side to be treated as a function expression instead of a function declaration.
It's a very unmon and unintuitive approach that I've never seen before.
It is more usual to wrap the function expression in parens or precede it with a not operator.
the var true = true; and var false = false; assignments in the 3-4 lines
The author appears to be trying to draw attention to the uses of true
and false
by copying them to variables that include non-alpha numerica characters in the name instead of using literals throughout. Again, this is very odd and not something I've ever seen before.
It looks like it is a collection of wrongly used "best practices" which not led to exceptions but definitely odd and obscured. Look at second and last lines. There is best practice used exactly vice versa:
(function ($, undefined){
// do the stuff
})(jQuery);
undefined here will be the real undefined because when function call there is no second argument. But what on Earth can be the reason pass the "undefined" argument to the function and do not use it? It looks like a prank.
The same thing is on 5 line: it looks (and actually acts) as "default argument" assigning but done in strange manner (traditionally and more obviously it used as location = location || {};
). I beleive that only reasons to write it that way it done can be obfuscation, joke or misunderstanding.
The same thing is with 100%
. You can use any operators to indicate function expression. The most mon way is to use parenthesis. But often you can also meet:
!function(){
}();
or:
+function(){
}();
but you can also write
42 * function(){
}();
it all acts the same way only parenthesis are most obvious and mon.
本文标签: idiomsStrange javascript codeStack Overflow
版权声明:本文标题:idioms - Strange javascript code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372350a2462481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论