日期:2014-05-16 浏览次数:20426 次
今天对js进行了小小的复习,以下为顺手敲的小例子。
<!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>test</title>
<script>
//Cookie封装
function dwn(s){
??? document.write(s+"<br/>");
}
//定义一个Cookie对象
function Cookie(){
??? this.set=function(name,value,expireTime){
??????? if(!expireTime) expireTime=new Date();
???????
??????? document.cookie=name+"="+value+";"+"expire="+expireTime.toGMTString();
??? };
??? //根据name获取cookie信息
??? this.get=function(name){
??????? var cookies=document.cookie.split("; ");
??????? for(var i=0;i<cookies.length;i++){
??????????? var s=cookies[i].split("=");
??????????? if(s[0]==name) return s[1];
??????? }
??? }
}
//新建一个Cookie对象
var cookie=new Cookie();
cookie.set("a","15");
cookie.set("b","25");
cookie.set("c","35");
//读取cookie值
dwn("b="+cookie.get("b"));
dwn("c="+cookie.get("c"));
/*
//join方法把一个数组所有元素转换成字符串
var a=[1,2,3];
var s=a.join();
alert(s);
//splice()方法是插入或删除数组元素的通用方法
var arr=[1,2,3,4,5,6,7];
alert(arr.splice(1,0,9));
alert(arr.join());
alert(arr.splice(1,0) instanceof Array);
*/
/*
var now = new Date();
alert(now.constructor);
*/
/*
//toLocaleString()本地化字符串
var now=new Date();
alert(now.toString());
alert(now.toLocaleString());
*/
//函数参数和函数返回值
/*function dwn(s){
??? document.write(s+"<br/>");
}
//集合变换操作,闭包作为参数
function trans(list,op){
??? for(var i=0;i<list.length;i++){
??????? list[i]=op(list[i]);
??? }
}
var list = [1,2,3,4];
trans(list,function(x){return x+1});//得到2,3,4,5
dwn(list);
trans(list,function(x){return x*2});
dwn(list);
//累加器:闭包作为返回值
function add(a,b){
??? b=b ||0;
??? var s = a+b;
??? var ret = function(a){
??????? return add(a,s);
??? }
??? ret.valueOf=ret.toString=function(){
??????? return s;
??? }
??? return ret;
}
dwn(add(5));
dwn(add(5)(10));
dwn(add(5)(10)(20));
*/
//用call和apply调用函数
/*function dwn(s){
??? document.write(s+"<br/>");
}
//定义一个Point类型
function Point(x,y){
??? this.x=x;
??? this.y=y;
??? this.toString=function(){
??????? return "("+[x,y]+")";
??? }
}
//定义一个Vector类型
function Vector(x,y){
??? this.x=x;
??? this.y=y;
??? this.toString=function(){
??????? return "["+[x,y]+"]";
??? }
}
//这个函数将传入的参数累加到对象的x,y属性上
function add(x,y){
??? return new this.constructor(this.x+x,this.y+y);
}
var p=new Point(1,2);
var v=new Vector(-1,2);
var p1= add.call(p,3,4);//把add函数作为p的方法调用
var v1= add.apply(v,[3,4]);//把add函数作为v的方法调用
dwn(p1);
dwn(v1);
*/
//打印方法中的参数
/*function f(){
??? document.write("f("+Array.apply(null,arguments)+")"+"<br/>");
}
f(1,2,3);
f("a","b");
f(true);
*/
//类型强制转换
/*function dwn(s){
??? document.write(s+"<br/>");
}
var str='100';
var fun = Function(str);
dwn(typeof(fun)+": "+fun);
//字符串类型强制转换
var num=Number(str);
dwn(typeof(num)+": "+num);
//对象类型强制转换
var obj={};
var str=String(obj);
dwn(typeof(str)+": "+str);
var num = Number(obj);
dwn(typeof(num)+": "+num);
var bool=Boolean(obj);
dwn(typeof(bool)+": "+bool);
*/
//闭包私有域
/*function dwn(s){
??? document.write(s+"<br/>");
}
var a,b;
(function(){
??? showAB = function(){
??????? dwn(a);
??????? dwn(b);
??? }
??? var a=10;
??? var b=20;
})();
a=-10;
b=-20;
dwn(a);
dwn(b);