javascript 函数和对象 再顺一顺

在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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值