Corsair

My javaWorld
数据加载中……
关于学习javaScript基础概念后写的示例代码
< script >
 @1 函数定义
function method(){
            var str="";
            for(var i=1;i<arguments.length;i++){
                    str+=arguments[i]
                }
            alert(str);
    }
method(1,2,3,4,5,6,7,8);
----------------

@2 随机函数
var method=new Function("x","y","var sum=0;sum=x+y;return sum;");
alert(method(1,2));
----------------

@3 对象定义
function obj(){}
var obj1=new obj();
obj1.name="corsiar";
obj1.age=22;
function method(x,y){
            return x+y;
    }
obj1.add=method;
alert(obj1["name"]);
alert(obj1.age);
alert(obj1.add(1,2));
----------------

@4 对象的定义2
function hero(name ,hp,mp){
            this.name=name;
            this.hp=hp;
            this.mp=mp;
            this.s1=show1;
            this.s2=show2;
    }
function show1(){
            alert(this.hp+100);
    }
function show2(){
            alert(this.mp+100);
    }
var corsair=new hero("corsair",100,300);
        alert(corsair.hp);
        alert(corsair.mp);
        corsair.s1();
        corsair.s2();
 

 @5 关于对象中with(对象名){}和for(变量in对象名){}的用法;
function person(name,age,sex,tel){
            this.name=name;
            this.age=age;
            this.sex=sex;
            this.tel=tel;
    }
function show(){
            alert(this.name);
            alert(this.sex);
            alert(this.tel);
    }
var p1=new person("corsair","22","boy","13778519215");
with(p1){
            var str="";
            str+=name+" ";
            str+=age+" ";
            str+=sex+" ";
            str+=tel+" ";
    }
alert(str);
var tmp,temp="";
for(tmp in p1){
        temp+=p1;
    }
alert(str);
 

</script>

posted on 2006-07-29 00:56 银河海盗 阅读(310) 评论(0)  编辑  收藏 所属分类: WEB


只有注册用户登录后才能发表评论。


网站导航: