您的位置:首页 > Web前端 > JavaScript

使用混合方式实现javascript中对象的继承(推荐使用)

2013-12-15 14:43 986 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>使用混合方式实现对象的继承</title>

    <script type="text/javascript">

        //使用混合方式实现对象的继承(推荐使用)

        function Parent(hello) {

            this.hello = hello;

        }

        Parent.prototype.sayHello = function() {

            alert(this.hello);

        }

        function Child(hello,world) {

            Parent.call(this, hello);

            this.world = world;

        }

        Child.prototype = new Parent();

        Child.prototype.sayWorld = function() {

            alert(this.world);

        }

        

        var child = new Child("hello", "world");

        child.sayHello();

        child.sayWorld();

    </script>

</head>

<body>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript