Web前端开发QQ群 110939958-业余程序员

[]==false之原理

问题来源

var a = [];

if(a) alert("true");//true

if(a == true) alert("true");//false

原理

js引擎会先计算括号内的表达式然后将结果转换为布尔值,所以

if(a) => if(!!a) => true

if(a == true) => if(Number(a) == true) => if(0 == true) => true

注: 在比较的两个值中,如果有一个是布尔类型,那么另外一个值将会先被转换为数字类型再与其比较。

参考: 双等号的比较原理

所以,下面这个就会有看起来奇怪的结果

[] == ![] //true

超长叠代

for(var i=0; i<1000000; i++) {
    //do some thing.
}

话说以上代码很可能会被浏览器判作执行超时。

试验了一下可以用浏览器的延时来做:

(function() {
    //do some thing.
    setTimeout(arguments.callee, 0);
})();


这样一来CPU倒是基本没有,但是这样的代码运行过慢,所以综合一下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title> new document </title>
  <script type="text/javascript">

	var interval = function(f, o) {
		var _onceRunTime = 5000;//每5秒一个新的任务队列
		var i = 0;
		(function() {
			var _st = new Date().getTime();
			while(true) {
				if(new Date().getTime() - _st > _onceRunTime) break;
				if(!f.call(o, i)) return;
				i++;
			}
			setTimeout(arguments.callee, 0);
		})();
	};
	function run(i) {
		document.getElementById("log").value = i;
		return i<1000000;
	}

	function test() {
		interval(run, this);
	}
  </script>
 </head>

 <body>
  <input type="text" id="log" /> <button id="b" onclick="test();">start</button>
 </body>
</html>

注意运行时也会使浏览器无反应,不过在每一个任务队列结束时仍然有机会刷新面。

MYSQL导入与导出命令

可以备份大量数据

1,数据库备份命令
mysqldump -h localhost -u root -p --default-character-set=utf8 dbname >backup.sql

2,导入数据命令:
mysql -h localhost -u root -p --default-character-set=utf8
use dbname
source backup.sql

双等号的比较原理

直接摘自ECMA262

章节号:11.9.3 The Abstract Equality Comparison Algorithm

比较 x == y, 当 x 和 y 都是值的时候, 产生truefalse. 这样的一个比较执行如下:

  1. If Type(x) is the same as Type(y), then
    1. If Type(x) is undefined, return true.
    2. If Type(x) is Null, return true.
    3. If Type(x) is Number, then
      1. If x is NaN, return false.
      2. If y is NaN, return false.
      3. If x is the same Number value as y, return true.
      4. If x is +0 and y is −0, return true.
      5. If x is −0 and y is +0, return true.
      6. Return false.
    4. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
    5. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
    6. Return true if x and y refer to the same object. Otherwise, return false.
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number,
    return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String or Number and Type(y) is Object,
    return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String or Number,
    return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

NOTE 1 鉴于以上相等的定义:
• 可以这样强制字符串比较: "" + a == "" + b.
• 可以这样强制数字比较: +a == +b.
• 可以这样强制布尔值比较:!a == !b.
NOTE 2 双等比较保持以下的不变式:
•  A != B 等价于 !(A == B).
•  A == B 等价于 B == A, 除了A和B求值的顺序不同
NOTE 3 The equality operator is not always transitive. For example, there might be two distinct String objects, each
representing the same String value; each String object would be considered equal to the String value by the == operator,
but the two String objects would not be equal to each other.
NOTE 4 Comparison of Strings uses a simple equality test on sequences of code unit values. There is no attempt to
use the more complex, semantically oriented definitions of character or string equality and collating order defined in the
Unicode specification. Therefore Strings values that are canonically equal according to the Unicode standard could test as unequal. In effect this algorithm assumes that both Strings are already in normalised form.

最近这部分待译

自定义事件之双击按键

遇到这样的需求,双击按键的事件
还是直接上代码吧:

//首先准备好常规的事件绑定函数
function addEvent(element, name, callback) {
	//标准浏览器
	if(element.addEventListener) element.addEventListener(name, callback, false);
	//IE
	else element.attachEvent("on" + name, callback);
}
//双击按键绑定函数
function addDblPressEvent(element, callback) {
	var presstime = 2;//次数为2
	addEvent(element, "keyup", press);
	//每次单击触发
	function press(e) {
		presstime--;
		setTimeout(reset, 300);
		if(presstime === 0) fire(e);
	}
	//恢复计数器
	function reset() {
		presstime++;
	}
	//触发双击
	function fire(e) {
		callback.call(element, e);
	}
}

//试一下
window.onload = function() {
	addDblPressEvent(document, function(e) {
		console.log("double clicked");
	});
}

system

我忘了是否从哪里看过,或者是否是经典,仅仅是突然想到的,就是这样的

系统

系统

function.delay

扩展一个函数的延时调用
Function.prototype.delay =  function(n, thisObj, args) {
    var me = this;
    args = args instanceof Array ? args : [];
    var f = function() {
        me.apply(thisObj || window, args);
    };
    window.setTimeout(f, n);
}
//示例
function f() {
    alert("hi");
}
alert("两秒之后弹出");
f.delay(2000);
注意第三个是参数数组
第一次用Live Writer写日志,呵呵