首先要说的是,javascript其实是可以进行OOP编程的,其次javascript的OOP编程实现方式有多种,我写的这一种只是我测试过,可行的一种
version1
// 父类function Person(name){ this.name = name}Person.prototype.say = saymyname;function saymyname(){alert(this.name)}// 子类function Employee(name){Person.call(this, name)} // 调用父类构造函数Employee.prototype = new Person(); // 继承父类Employee.prototype.cardID = function(){alert("1000")} // 定义子类自己的方法// 实例化p = new Employee("lishujun");p.say();p.cardID();
version2
// 父类function Person(name){ this.name = name}Person.prototype.say = saymyname;function saymyname(){alert(this.name)}// 子类function Employee(name){Person.call(this, name)} // 调用父类构造函数Employee.prototype = new Person(); // 继承父类Employee.prototype.cardID = function(){ this.say(); alert("1000")} // 定义子类自己的方法,调用父类方法// 实例化p = new Employee("lishujun");p.say();p.cardID();
version3
// 父类function Person(name){ this.name = name}Person.prototype.say = saymyname;function saymyname(){alert(this.name)}// 子类function Employee(name){Person.call(this, name)} // 调用父类构造函数Employee.prototype = new Person(); // 继承父类Employee.prototype.cardID = function(){alert("1000")} // 定义子类自己的方法Employee.prototype.say = function(){alert("Employee say")} // 覆盖父类方法,即:多态// 实例化p = new Employee("lishujun");p.say();p.cardID();