JavaScript对象(正则表达式,Date对象,function对象 arguments对象)

好用的技术教程:http://www.w3school.com.cn/index.html

1:正则表达式

正则表达式通常用于验证表单

定义语法为 / /

2:Date对象

var now = new Date();以及很多的tostring方法

3:function对象 arguments对象,用于实现JavaScript的重载

在JavaScript中,如果方法名相同,只能执行最后定义的方法,因为在JavaScript中,方法可以用

var myMethod = function(n);

var myMethod = function(n,m);

方法的本质是变量,所以,只能保存最后一次定义的方法;

通过function和arguments对象的组合,实现类似于Java的方法重载:

复制代码
function myMethod(){
    if(arguments.length ==1){
        var n = parseInt(arguments[0]);
        alert(n*n);
        }
    else {
        if(arguments.length ==2){
        var n = parseInt(arguments[0]);
        var m = parseInt(arguments[1]);
        alert(n,m);
        }
        }
    }
复制代码

 

全局函数:不需要对象的函数

服务器的编码和解码:

<script>
    var str ="我爱小逗比" ;
    document.write("<h1>str="+str+"</h1>");
    str= encodeURI(str);
    document.write("<h1>encodeURI:str="+str+"</h1>");
    str= decodeURI(str);
    document.write("<h1>decodeURI:str="+str+"</h1>");    
</script>

实验现象:

 函数eval()用于计算字符串

复制代码
<!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>计算器</title>
<script src="计算器.js"></script>
</head>

<body>
<input type="text" id="txtData" readonly="readonly"/>
</br>
<input type="button" value="1" onclick="calc(this.value)"/>
<input type="button" value="2" onclick="calc(this.value)"/>
<input type="button" value="3" onclick="calc(this.value)"/>
</br>
<input type="button" value="4" onclick="calc(this.value)"/>
<input type="button" value="5" onclick="calc(this.value)"/>
<input type="button" value="6" onclick="calc(this.value)"/>
</br>
<input type="button" value="7" onclick="calc(this.value)"/>
<input type="button" value="8" onclick="calc(this.value)"/>
<input type="button" value="9" onclick="calc(this.value)"/>
</br>
<input type="button" value="+" onclick="calc(this.value)"/>
<input type="button" value="-" onclick="calc(this.value)"/>
<input type="button" value="=" onclick="calc(this.value)"/>
<input type="button" value="c" onclick="calc(this.value)"/>




</body>
</html>
View Code
复制代码

JavaScript:

复制代码
// JavaScript Document
function calc(str){
    var txtObj = document.getElementById("txtData");
    switch(str){
        case "c":
            txtObj.value = " ";
        break;
        case "=":
            var input = txtObj.value;
            if(input.length>0)
                try{
                    var r=eval(input);
                    txtObj.value=txtObj.value+"=";
                    txtObj.value +=+r;
                    }
                catch(error){
                    alert("不符合算术规则");
                    }
            
        break;
        default:
        txtObj.value +=str;
        
        break;
        }
}
View Code
复制代码

代码效果:

 

posted @   跨七海的风  阅读(180)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示