[javascript]一种比较通用的js继承过程

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        function Person(name, gender) {
            this.name = name;
            this.gender = gender;
        }
        Person.prototype.showName = function() {
            alert(this.name);
        }
        Person.prototype.showGender =function() {
            alert(this.gender);
        }
  
        function Worker(name, gender, job) {
       // Step1 Person.call(
this, name, gender); this.job = job; }    // Step2 for (var i in Person.prototype) { Worker.prototype[i] = Person.prototype[i]; } Worker.prototype.showJob = function() { alert(this.job); } var oW1 = new Worker("hello", "male", "coder"); oW1.showJob(); </script> </body> </html>

 

posted @ 2015-07-26 23:04  钟山  阅读(139)  评论(0编辑  收藏  举报