javascript的几种继承方法

<script type="text/javascript">
    function Person(name, address) {
        this.Name = name;
        this.Address = address;
    }
    Person.prototype.Show = function() {
        return this.Name;
    }

    //使用call 继承
    function user(name, address) {
        Person.call(this, name, address)
        this.Tel = "134010842**";
    }

    //使用apply 继承
    function user2(name, address) {
        Person.apply(this, arguments) //apply第二个参数是参数数组,所以可以使用arguments(这是和call的区别)
        this.Tel = "134010842**";
    }

    //使用prototype 继承
    user3.prototype = new Person(); //建一个基类的对象作为子类原型的原型,这样子类才会继承父类的方法
    function user3(name, address) {
        Person.apply(this, arguments);
        this.Tel = "134010842**";
    }

    var user1 = new user("江湖小子1", "四川");
    alert(user1.Name + "-" + user1.Tel + "-" + user1.Address);

    var user2 = new user2("江湖小子2", "北京");
    alert(user2.Name + "-" + user2.Tel + "-" + user2.Address);

    var user3 = new user3("江湖小子3", "上海");
    alert(user3.Name + "-" + user3.Tel + "-" + user3.Address);
</script>

Javascript面向对象:

<script type="text/javascript">
    var Bus;
    //构造函数
    if (Bus == undefined) {
       Bus = function(name, num) {
           this.Init(name, num);
       };
    }
    Bus.prototype.Init = function(name, num) {
       this.Name = name;
       this.Num = num;
    }
    //静态成员
    Bus.City = "静态北京";
    Bus.GetCity = function() {
        return Bus.City;
    }

    //实例成员
    Bus.prototype.City = "实例北京";
    Bus.prototype.Show = function() {
        return this.Name + "-" + this.Num;
    }

    var bus = new Bus("公交车", "100");
    alert(bus.City);        //实例北京
    alert(bus.Show());      //公交车 - 100
    alert(Bus.City);        //静态北京
    alert(Bus.GetCity());   //静态北京
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值