当前位置: 代码迷 >> JavaScript >> js温习
  详细解决方案

js温习

热度:102   发布时间:2012-11-22 00:16:41.0
js复习

今天对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);
showAB();//不会破坏showAB()内部的a,b的值
*/

//定义5个方法,(function)(),表示声明完立即执行
//j的值是由最后的那个i传入
//setTimeout中是匿名方法
/*function test(){
??? for(var i=0;i<5;i++){
??????? (function(j){
??????????? setTimeout(function(){alert(j)},100);
??????? })(i);
??? }
}
test();*/

//闭包和面向对象
/*function dwn(s){
??? document.write(s+"<br/>");
}
function Foo(a){
??? function _pc(){
??????? return a;
??? }
??? this.bar = function(){
??????? dwn("foo "+_pc()+"!");
??? }
}
var obj = new Foo("bar");
obj.bar();*/


//闭包的封闭性
/*function dwn(s){
??? document.write(s+"<br/>");
}
(function(){
??? //封闭私有域
??? var innerX = 10,innerY=20;
??? //开放公共域
??? outerObj = {x:innerX,y:innerY};
})();

try{
??? dwn(innerX);
}catch(ex){
??? dwn("内部数据无法访问");
}
dwn(outerObj.x);*/


//闭包和面向对象
/*function dwn(s){
??? document.write(s+"<br/>");
}
function Foo(a){
??? function _pc(){//私有函数
??????? return a;
??? }
???
??? this.bar=function(){
??????? dwn("foo"+_pc()+"!");
??? }
}
var obj=new Foo("bar");
obj.bar();*/

//闭包的封闭性
/*function dwn(a){
??? document.write(s+"<br/>");
??? alert(0);
}
(function(){
??? //封闭的私有域
??? var innerX=10,innerY=20;
??? //开放的公共域
??? outerObj={x:innerX,y:innerY};
})();
try{
??? dwn(innerX);//内部数据无法访问
}catch(ex){
??? dwn("内部数据无法访问!");
}
dwn(outerObj.x);//通过外部接口访问
*/

//执行域
/*function RandomAlert(){
??? var x= Math.random();
??? return function(){
??????? alert(x);
??? }
}
var a=RandomAlert();
a();
*/

<!-- 拆箱、装箱 -->
/*
function dwn(s){
??? document.write(s+"<br/>");
}
//Number装箱
Number.prototype.foo=function(){
??? dwn(this instanceof Number);
??? dwn(typeof(this));
}
//定义基本类型
var num=10;
num.foo();

//Number拆箱
var objNum=new Number(10);
dwn(objNum.valueOf() instanceof Number);
dwn(typeof(objNum.valueOf()));

//String装箱
String.prototype.foo=function(){
??? dwn(this instanceof String);
??? dwn(typeof(this));
}
//定义字符串
var str = "www.51js.com";
str.foo();

//String拆箱
var objStr = new String("abc");
dwn(objStr instanceof String);
dwn(typeof(objStr.toString()));
*/

<!--? 值和引用 -->
/*function dwn(s){
??? document.write(s+"<br/>");
}
var va=10,vb=true;
var ra=[1,2,3],rb={x:1,y:2};
//参数为值类型的函数例子
function ValueTypes(x,y){
??? x++;
??? y=false;
??? dwn(x);
??? dwn(y);
}
//参数为引用类型的函数例子
function ReferTypes(x,y){
??? x.push(4);
??? delete y.x;
??? dwn(x);
??? dwn(y.x);
}
ValueTypes(va,vb);
dwn(va);
dwn(vb);
ReferTypes(ra,rb);
dwn(ra);
dwn(rb.x);*/

<!--? 继承 -->
/*
function dwn(s){
??? document.write(s+"<br/>");
}
//定义一个Animal类型
function Animal(){
??? this.bite=function(){
??????? dwn("animal bite!");
??? }
}
//定义一个Cat类型,继承Animal类型
function Cat(){
??? this.bite = function(){
??????? dwn("cat bite");
??? }
}
Cat.prototype=new Animal();

//定义一个Dog类型,继承Animal类型
function Dog(){
??? this.bite = function(){
??????? dwn("dog bite!");
??? }
}
Dog.prototype = new Animal();
//定义一个AnimalBite方法
function AnimalBite(animal){
??? if(animal instanceof Animal){
??????? animal.bite();
??? }
}
var cat = new Cat();
var dog = new Dog();
AnimalBite(cat);
AnimalBite(dog);
*/
</script>

</head>

<body>
</body>
</html>

  相关解决方案