1.json转字符串
复制代码 代码如下:
function json2str(o) {
var arr = [];
var fmt = function (s) {
if (typeof s == 'object' && s != null) return json2str(s);
return /^(string|number)$/.test(typeof s) ? ' + s + ' : s;
};
for (var i in o) arr.push(' + i + ': + fmt(o[i]));
return '{' + arr.join(',') + '}';
}
2.时间戳转为date
复制代码 代码如下:
function fromunixtime(timestamp) {
if (!timestamp || timestamp var thedate = new date(parseint(timestamp) * 1000);
return thedate;
}
3.data-format
复制代码 代码如下:
// 作者: meizz
// 对date的扩展,将 date 转化为指定格式的string
// 月(m)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(s)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new date()).format(yyyy-mm-dd hh:mm:ss.s) ==> 2012-12-02 08:12:04.423
// (new date()).format(yyyy-m-d h:m:s.s) ==> 2012-12-02 8:12:4.18
date.prototype.format = function(fmt) {
var o = {
m+: this.getmonth() + 1, //月份
d+: this.getdate(), //日
h+: this.gethours(), //小时
m+: this.getminutes(), //分
s+: this.getseconds(), //秒
q+: math.floor((this.getmonth() + 3) / 3), //季度
s: this.getmilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(regexp.$1, (this.getfullyear() + ).substr(4 - regexp.$1.length));
for (var k in o)
if (new regexp(( + k + )).test(fmt))
fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k]) : ((00 + o[k]).substr(( + o[k]).length)));
return fmt;
};
4.日期上增加n天
复制代码 代码如下:
function addday(number) {
return fromunixtime(new date().gettime() / 1000 + 24 * 60 * 60 * number);
}
5. 使用 iframe 时,父窗体与子窗体之间的相互调用
复制代码 代码如下:
// 父窗体调用子窗体内的函数
window.frames['ifm_id'].valuechange(id_101);
// 子窗体调用父窗体的函数
parent.refreshtree(nodeid_202);
6. 弹出窗体与返回值
复制代码 代码如下:
// 弹出窗体
var url = http://www.baidu.com;
win=window.showmodaldialog(url,window,dialogleft:400;dialogtop:200;dialogwidth:560px;dialogheight:380px;scroll:yes;menubar:no;toolbar:no;status:no;);
// 在弹出窗体中设置返回值
var result = new array();
result[0] = id_101;
result[1] = name_202;
window.returnvalue = result;
window.close();
7. javascript 作用域[只有全局作用域和函数作用域,javascript没有块作用域]
复制代码 代码如下:
// 1. 全局作用域
var id = global variable; // 1.1 在函数外部定义的变量
function showmsg(){
message = global message;// 1.2 未定义而直接赋值的变量
// 在第一次使用时被定义为全局变量
}
// 2. 函数作用域
function docheck(){
var data = function data;// 2.1 在函数内部定义的变量
}
8. javascript 继承机制
复制代码 代码如下:
// 1. 对象冒充继承
function person(strname){
// private fields
var name = strname;
// public methods
this.getname = function(){
return name;
};
}
function student(strname,strschool){
// 定义父类的属性及方法
this.parent = person;
this.parent(strname);
delete this.parent; // 删除临时变量 parent
// 定义新属性及方法
// private fields
var school = strschool;
// public methods
this.getschool = function(){
return school;
};
}
// 2. funtion 对象的 call(..) 或 apply(..) 继承
// call 和 apply 的区别在于:
// call 的第二个参数为可变参数;
// apply 的第二个参数为 array;
function animal(strname,intage){
// private fields
var name = strname;
var age = intage;
// public methods
this.getname = function(){
return name;
};
this.getage = function(){
return age;
};
}
function cat(strname,intage,strcolor){
// 定义父类的属性及方法
animal.call(this,strname,intage);
// animal.apply(this,new array(strname,intage));
// 定义新属性及方法
// private fields
var color = strcolor;
// public methods
this.getinfo = function(){
return name: + this.getname() + \n
+ age: + this.getage() + \n
+ color: + color;
};
}
// 3. prototype 继承
// prototype 声明的属性及方法被所有对象共享
// prototype 只有在读属性的时候会用到
function.prototype.extend = function(superclass){
// 此处的 f 是为了避免子类访问父类中的属性 this.xxx
function f(){};
f.prototype = superclass.prototype;
// 父类构造函数
this.superconstructor = superclass;
this.superclass = superclass.prototype;
this.prototype = new f();
this.prototype.constructor = this;
};
function.prototype.mixin = function(props){
for (var p in props){
this.prototype[p] = props[p];
}
};
function box(){}
box.prototype = {
gettext : function(){
return this.text;
},
settext : function(text){
this.text = text;
}
};
function checkbox(){}
checkbox.extend(box);
checkbox.mixin({
ischecked : function(){
return this.checked;
},
setchecked : function(checked){
this.checked = checked;
}
});
9. call , apply & bind
复制代码 代码如下:
// thisarg 表示在 fun 内部时 this 所指示的对象
// call & apply 将立即执行 fun 并返回结果
var result = fun.call(thisarg,arg1,...);
var result = fun.apply(thisarg,[argsarray]);
// thisarg 表示在 fun 内部时 this 所指示的对象
// bind 返回的是一个匿名函数
var tmpfun = fun.bind(thisarg);
var result = tmpfun(arg1,...);
复制代码 代码如下:
10. js == operator
复制代码 代码如下:
转换规则
如果一个操作数是 boolean 值,则比较之前将其转成数字:false -> 0, true -> 1;
如果一个操作数是数字,另一操作数是字符串,则比较之前将字符串转成数字;
如果一个操作数是对象,另一操作数是数字或字符串,则比较之前会将对象转为基本类型,
引擎会先尝试调用 valueof(),如果 valueof() 没有 override 或返回一个对象,
则引擎会尝试调用 tostring(),如果 tostring() 没有 override 或返回一个对象,则抛出异常;
如果是两个对象进行比较,则判断它们是否引用同一对象;
如果一个操作数是 nan, == 将返回 false, != 将返回 true;
null 和 undefined 与其它值比较将返回 false,
但 null == null, undefined == undefined, null == undefined;
参与比较时 null 和 undefined 不能转为其它值;