var loadScript = function(src,callback,cache){
var s=document.createElement("script");
s.type="text/javascript";
s[document.all?"onreadystatechange":"onload"] = function(){
if(document.all&&this.readyState!="loaded"&&this.readyState!="complete"){
return;
}
this[document.all?"onreadystatechange":"onload"]=null;
this.parentNode.removeChild(this);
if(callback != undefined) callback();
};
if(!cache) src += (/?/.test(src)?"&":"?") + "_=" + (new Date()).getTime();
s.src = src;
document.getElementsByTagName("head")[0].appendChild(s);
}
注意的问题:
1. Firefox下的onload事件是在正常加载完毕后出发的,如果是404或500等错误,则不会触发onload事件,这时回调函数就没时候执行了。
使用memcached时需要注意的几个问题
1. 对于已经运行着的memcached,如果要在里面存放其他的东西,需要注意这item的大小是否和原来存放的item的大小相当,如果不相当,同时memcached的内存已经开辟完了,则无法开辟新的空间给新的item使用,因为开辟出去的内存是不会自动回收的。
2. 尽量不要使用set来删除内容,使用set方法删除的item是不会优先使用的,只有内存都开辟完了才会使用,所以使用set来删除的话,内存开辟的很快的,你将不知道有多少内存是有效利用的
javascript 中正则的单行与多行
一般情况下我们都是用正则来处理一行字符串,如果要处理多行的字符串怎么办呢?
对于函数:
function a(){
…
}
是一个多行的字符串,要取函数名,怎么写?
方法一:
因为函数名肯定出现在第一行,所以可以通过: /^function +([^(]*)/)[1] 来获取
方法二:
将函数名前面的字符和后面的字符都替换掉:
.replace(/^function +/,"").replace(/\((.|\n)*/m,"");
注意不要写成:
.replace(/^function +/,"").replace(/\([.\n]*/m,"");
因为
1. dot 在 [] 里面是不能表示任意字符的
2. dot 也不包含回车
3. 注意m修改选项
javascript 中判断数据类型(constructor)
一般情况下我们都用typeof来判断数据类型,但是javascript 中任何一个东西都是对象,如:
var a = new Array();
alert(typeof a); // object
var b = new Date();
alert(typeof b); // object
于是我们就只知道a b都是object,但是不知道这个对象究竟是什么名字,这时候constructor就可以帮助我了:
var a = new Array();
alert(a.constructor); // function Array(){..}
var b = new Date();
alert(b.constructor); // function Date(){..}
比object具体多了,但是表达方式不太友好,用正则替换一下就行了:
var a = new Array();
alert(a.constructor.toString().match(/^function ([^(]*)/)[1]); // Array
var b = new Date();
alert(b.constructor.toString().match(/^function ([^(]*)/)[1]); // Date
javascript 中创建对象的几种方法
1. 最简单的方法
var obj = {};
obj.func1 = function(){alert("func1");};
2.
var Class = {
create: function() {
return function() { this.initialize.apply(this, arguments); }
}
}
// 这是prototype.js里面的类构造方法,使用方法是:
var myClass = Class.create();
// 这样就可以创建一个类了。
// 继续加入:
myClass.prototype = {
x : function () { return 1; },
y : function () { return 2; },
initialize : function () { alert(this.x() + this.y()); }
}
// 那么,你在实例化该类的时候:
var mclass = new myClass();
// 此时会弹出一个对话框,相当于:alert(1 + 2);
3. 定义函数的方式
var mycalss = function(){
// 这里写什么东西在new的时候都会执行,相当于构造函数了
this.method1 = function(){
//…
};
// 或者写一个方法然后调用
this.initailize = function(){…};
this.initailize();
// 或者: this.initailize.apply(this,arguments); 不知道和直接调用有多大差别
};
4.
var myclass = {
method1:function(){…},
method2:function(){…},
property1:"string"
};
linux信号机制
http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index1.html
Javascript 调试工具 之 Firebug
参考文章:http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx
javascript 实现的阶乘函数
// Y 函数实现的太绕了,太难以理解了, 无语。
function factorial(proc) {
return function (n) {
return (n <= 1) ? 1 : n * proc(n-1);
}
}
function Y(outer) {
function inner(proc) {
function apply(arg) {
return proc(proc)(arg);
}
return outer(apply);
}
return inner(inner);
}
alert("5! is " + Y(factorial)(5));
命令行javascript
javascript在浏览器中用的算是很泛滥了,但是有时候我们就想像其他语言一样脱离浏览器使用,windows上提供了运行javascript的脚本宿主,可以直接运行js文件;但是Linux用多了就希望什么事情都在Linux上实现,网上找了一下,还真有Linux上的js解释器,是mozilla提供的:
http://ftp.mozilla.org/pub/mozilla.org/js/
使用说明: https://developer.mozilla.org/en/SpiderMonkey_Build_Documentation
可以同时执行js文件和js语句,如:
js -f Y.js -e "print(Y);"
安装方法:
——————————–
1 |
<span style="color: rgb(102, 102, 102); font-style: italic;"># 安装。完成之后会有/usr/bin/js命令</span><br /><span style="color: rgb(194, 12, 185); font-weight: bold;">sudo</span> <span style="color: rgb(194, 12, 185); font-weight: bold;">apt-get</span> <span style="color: rgb(194, 12, 185); font-weight: bold;">install</span> spidermokey-bin<br /> <br /><span style="color: rgb(102, 102, 102); font-style: italic;"># 测试,执行命令</span><br />js <span style="color: rgb(102, 0, 51);">-e</span> <span style="color: rgb(255, 0, 0);">"var s='www.codigg.com';print(s.length);"</span><br /> <br /><span style="color: rgb(102, 102, 102); font-style: italic;"># 执行文件</span><br /><span style="color: rgb(122, 8, 116); font-weight: bold;">echo</span> <span style="color: rgb(255, 0, 0);">"var s='www.codigg.com';print(s.length);"</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">></span> test.js<br />js <span style="color: rgb(102, 0, 51);">-f</span> test.js<br /> <br /><span style="color: rgb(102, 102, 102); font-style: italic;"># 脚本和文件结合</span><br />js <span style="color: rgb(102, 0, 51);">-f</span> test.js <span style="color: rgb(102, 0, 51);">-e</span> <span style="color: rgb(255, 0, 0);">"print(100+s.length)"</span> |
——————————————————————————–
=============================================
1 |
<span style="color: rgb(194, 12, 185); font-weight: bold;">wget</span> http:<span style="color: rgb(0, 0, 0); font-weight: bold;">//</span>ftp.mozilla.org<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>pub<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>mozilla.org<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>js<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>js-1.8.0-rc1.tar.gz<br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">tar</span> <span style="color: rgb(102, 0, 51);">-xf</span> js-1.8.0-rc1.tar.gz<br /> <span style="color: rgb(122, 8, 116); font-weight: bold;">cd</span> js<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>src<br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">make</span> <span style="color: rgb(0, 120, 0);">BUILD_OPT</span>=<span style="color: rgb(0, 0, 0);">1</span> <span style="color: rgb(102, 0, 51);">-f</span> Makefile.ref<br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">mkdir</span> <span style="color: rgb(102, 0, 51);">-p</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>include<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>smjs<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">cp</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">*</span>.<span style="color: rgb(122, 8, 116); font-weight: bold;">{</span>h,tbl<span style="color: rgb(122, 8, 116); font-weight: bold;">}</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>include<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>smjs<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><br /> <span style="color: rgb(122, 8, 116); font-weight: bold;">cd</span> Linux_All_DBG.OBJ<br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">cp</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">*</span>.h <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>include<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>smjs<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">mkdir</span> <span style="color: rgb(102, 0, 51);">-p</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>local<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><span style="color: rgb(122, 8, 116); font-weight: bold;">{</span>bin,lib<span style="color: rgb(122, 8, 116); font-weight: bold;">}</span><span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">cp</span> js <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>local<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>bin<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span><br /> <span style="color: rgb(194, 12, 185); font-weight: bold;">cp</span> libjs.so <span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>usr<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>local<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span>lib<span style="color: rgb(0, 0, 0); font-weight: bold;">/</span> |
=============================================
超实用的几种速算技巧
(1)58×52
(2)67×47
(3)66×91
(4)98×97
解:(1)58×52(十位数相同、个位数互补)
=(5+1)×5×100+8×2
=30×100+16
=3000+16
=3016
十位数相同,个位数互补的简便方法是:首位(“5”即十位数)加1 的和
再乘以首位数作为积的前两位数;末位数(即个位数)相乘的积作为积的后两位数.
(2)67×47(个位数相同、十位数互补)
=(6×4+7)×100+7×7
=31×100+49
=3100+49
=3149
个位数相同,十位数互补的速算方法是:首位(“ 6”)乘以首位(“4”)
再加上个位数作为积的前两位(即:6×4+7=31),末位数乘以末位数(个位
数)的积(7×7)作为积的后两位数.
(3)66×91
=(6×9+6)×100+1×6
=60×100+6
=6000+6
=6006
一个因数是11 的倍数,另一个因数个位和十位数字互补(“9”和“1”).
速算方法是:首位数(即十位数)乘以首位数,再加上相同数中的一个数作为
积的前两位,末位数乘以末位数的积作为积的后两位数.
(4)98×97
=〔98-(100-97)〕×100+(100-98)×(100-97)
=〔98-3〕×100+2×3
=95×100+6
=9500+6
=9506
一个因数减去另一个因数的补数(98-3)作为积的前两位数(95);两个因
数补数的乘积作为积的后两位数.
(1)9999^2+19999(把一个数分解成两个数的和)
(2)34999965÷35(把一个数分解成两个数的差)
3)1991×19921992-19911991×1992
(4)33333×33333
解:(1)9999^2+19999
=9999^2+9999+10000
=9999×(9999+1)+10000
=9999×10000+10000
=10000×(9999+1)
=10000×10000
=100000000
(2)34999965÷35
=(35000000-35)÷35
=35000000÷35-35÷35
=1000000-1
=999999
(3)1991×19921992-19911991×1992
=1991×1992×10001-1991×10001×1992
=1991×1992×(10001-10001)
=1991×1992×0
=0
(4)33333×33333
=11111×3×33333
=11111×99999
=11111×(100000-1)