javascript中的對象繼承的方法有很多,在接下來的文章中為大家介紹下使用apply方法是如何實現的
代碼如下: <script type="text/javascript"> //使用apply方法實現對象繼承 function Parent(username) { this.username = username; this.sayHello = function() { alert(this.username); } } function Child(username, password) { Parent.apply(this, new Array(username)); //和下面一樣 //Parent.apply(this, [username]); this.password = password; this.sayWorld = function() { alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi", "123"); parent.sayHello(); child.sayHello(); child.sayWorld(); </script>