javascript实现继承

来源:互联网 发布:江苏悠迅网络是干嘛的 编辑:程序博客网 时间:2024/06/05 10:03
//javascript通过将构造器函数与原型对象相关联的方式来实现继承    function Employee(name,dept){        this.name=name;        this.dept = dept;    }    function Manager(reports){        this.reports = reports;    }    Manager.prototype=new Employee();    function Employee(){        this.name = "";        this.dept = "general";    }    function Manager(){        this.reports = [];    }    Manager.prototype = new Employee;    function WorkerBee(){        this.projects = [];    }    WorkerBee.prototype=new Employee;    function SalesPerson(){        this.dept = "sales";        this.quota = 100;    }    SalesPerson.prototype=new WorkerBee;    function Engineer(){        this.dept="engineering";        this.machine="";    }    Engineer.prototype=new WorkerBee;    var jim = new Employee();    console.log(jim.name);    console.log(jim.dept);    var sally = new Manager;    console.log(sally.name);    console.log(sally.dept);    console.log(sally.reports);    var mark = new WorkerBee;    console.log(mark.name);    console.log(mark.dept);    console.log(mark.projects);    var fred = new SalesPerson;    console.log(fred.name);    console.log(fred.dept);    console.log(fred.projects);    console.log(fred.quota);    var jane = new Engineer;    console.log(jane.name);    console.log(jane.dept);    console.log(jane.projects);    console.log(jane.machine);

0 0
原创粉丝点击