日期:2014-05-16  浏览次数:20498 次

JS复习二
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN">
<head>
<title>JS Study</title>
<meta charset="utf-8">
<style>
    .userData {behavior:url(#default#userdata);}
</style>
<script type="text/javascript">
    //组合使用构造函数模式和原型模式
    function Person(name,age,job){
        this.name=name;
        this.age=age;
        this.job=job;
        this.friends=["Shelby","Court"];
    }
    Person.prototype={
        constructor:Person,
        sayName:function(){
            alert(this.name);
        }
    }
    var person1=new Person("test",20,"software");
    var person2=new Person("test2",22,"software2");
    person1.friends.push("Van");
    alert(person1.friends);
    alert(person2.friends);
    alert(person1.friends===person2.friends);//false
    alert(person1.sayName===person2.sayName);//true
    
    /*
    //原型对象问题
    function Person(){
        
    }
    Person.prototype={
        constructor:Person,
        name:"test",
        age:20,
        job:"IT",
        friends:["Shelby","Court"],
        sayName:function(){
            alert(this.name);
        }
    };
    var person1=new Person();
    var person2=new Person();
    person1.friends.push("Van");
    alert(person1.friends);//Shelby,Court,Van
    alert(person2.friends);//Shelby,Court,Van
    alert(person1.friends===person2.friends);//true
     */
    
    /*
    //构造函数模式
    function Person(name,age,job){
        this.name=name;
        this.age=age;
        this.job=job;
        this.sayName=function(){
            alert(this.name);
        };
    }
    var person1=new Person("test",20,"IT");
    person1.sayName();
     */
    
    /*
    //Math对象
    var max=Math.max(3,54,32,16);
    alert(max);
    var min=Math.min(3,54,32,16);
    alert(min);
    //Math舍入方法
    //ceil()向上舍入
    //floor()向下舍入
    //round()四舍五入
    alert(Math.ceil(25.9));//26
    alert(Math.floor(25.5));//25
    alert(Math.round(25.6));//26
     */
    
    /*
    //Global全局对象
    //eval()方法
    eval("alert('hi')");
    eval("function sayHi(){alert('hi')}");
    sayHi();
     */
    
    /*
    //编码
    var uri="http://www.english hello.com";
    //编码后除了空格之外的其他字符都原封不动,只有空格被替换成了%20 http://www.english%20hello.com 
    alert(encodeURI(uri));
    //对所有非字母数字编码
    alert(encodeURIComponent(uri));//http%3A%2F%2Fwww.english%20hello.com
    
    //解码
    var uri2="http%3A%2F%2Fwww.english%20hello.com";
    alert(decodeURI(uri));
    alert(decodeURIComponent(uri));
     */
    
    /*
    var text="cat, bat, sat, fat";
    var pos=text.search(/at/);
    alert(pos);//1
     */
    
    /*
    //String中的slice()、substr()和substring()方法
    var stringValue="hello world";
    alert(stringValue.slice(3));//lo world
    alert(stringValue.slice(3,7));//lo w
    alert(stringValue.substring(3,7));//lo w
    alert(stringValue.substr(3,7));//lo worl
     */
    
    /*
    //call()、apply()方法最强大的地方是能够扩充函数赖以运行的作用域
    window.color="red";
    var o={color:"blue"};
    function sayColor(){
        alert(this.color);
    }
    sayColor();//red
    sayColor.call(this);//red
    sayColor.call(window);//red
    sayColor.call(o);//blue
     */
    
    /*
    //call()方法
    function sum(num1,num2){
        return num1+num2;
    }
    function callSum(num1,num2){
        return sum.call(this,num1,num2);
    }
    alert(callSum(10,10));
     */
    
    /*
    //apply方法
    function sum(num1,num2){
        return num1+num2;
    }
    function callSum1(num1,num2){
        return sum.apply(this,arguments);//传入arguments对象
    }
    function callSum2(num1,num2){
        return sum.apply(this,[num1,num2]);//传入数组
    }
    alert(callSum1(10,10));//20
    alert(callSum2(10,10));//20
     */
    
    /*
    //compare排序
    function compare(value1,value2){
        if(value1<value2)