javascript 函数和对象 再顺一顺

来源:互联网 发布:软件项目管理体系xp 编辑:程序博客网 时间:2024/06/05 06:57

在javascript 中 函数也是对象 ,当然对象更是对象,是复杂类型,对于初学者或者自学者 总有一种变来变去的感觉 别的不说了看码

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title></head><script>    var ob={  //创建一个对象        age:10,        name:"sdfsdf",        show:function(){            document.writeln(this.age+" "+this.name);        }    }    var bb=function(){}//创建一个函数    bb.names="admin";//吧函数当成一个对象 添加属性和方法    bb.age=30;    bb.show=function(){        document.writeln(this.names+" "+this.age);    };    var fn=function(name,psw){  //有参数的函数        this.name=name;        this.psw=psw;        this.show=function(){            document.writeln(this.name+" "+this.psw);        }    }    var t=new fn(25,"admin");    fn.prototype.run=function()  //对fn 函数再添加一个方法    {        document.writeln(this.name+" "+this.psw);    }</script><body><input type="button" value="object" onclick="ob.show()"><input type="button" value="function->object" onclick="bb.show()"><input type="button" value="function" onclick="t.show()"><input type="button" value="function prototype" onclick="t.run()"></body></html>



0 0