在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" οnclick="ob.show()">
<input type="button" value="function->object" οnclick="bb.show()">
<input type="button" value="function" οnclick="t.show()">
<input type="button" value="function prototype" οnclick="t.run()">
</body>
</html>