Last active
December 17, 2016 14:57
-
-
Save yangfch3/24d0eb5293088824d25df4e26856b88d to your computer and use it in GitHub Desktop.
一段讲解使用 JS 实现 OOP 的代码实例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inheritPrototype(subType, superType) { | |
if (Object.create) { | |
// 利用 Object.create 的特性实现 __proto__ 指针的正确指向,完美 | |
subType.prototype = Object.create(superType.prototype); | |
subType.prototype.constructor = subType; | |
} else { | |
function F() {} | |
F.prototype = superType.prototype; | |
var prototype = new F(); | |
prototype.constructor = subType; | |
subType.prototype = prototype; | |
} | |
} | |
var Employee = (function(){ | |
var _next_id = 1; | |
var _employeeCount = 0; | |
function Employee(name, salary, year, month, day) { | |
var _this = this; | |
var _id = _next_id; | |
_this.name = name; | |
_this.salary = salary; | |
_this.post = "Employee"; | |
_this.hireDay = new Date(year, month, day); | |
_next_id++; | |
_employeeCount++; | |
} | |
Employee.prototype.getId = function () { | |
return this._id; | |
}; | |
Employee.prototype.setId = function () { | |
alert("员工ID由系统自动生成,不能手动修改"); | |
return false; | |
}; | |
Employee.prototype.computeSeniority = function() { | |
return (new Date().getFullYear() - this.hireDay.getFullYear()); | |
}; | |
Employee.getNextId = function() { | |
return _next_id; | |
}; | |
Employee.setNextId = function(value) { | |
alert("Next ID 由系统自动生成,不能手动修改"); | |
return false; | |
}; | |
Employee.getCounts = function() { | |
return _employeeCount; | |
}; | |
return Employee; | |
})(); | |
var Manager = (function(){ | |
var _managersCount = 0; | |
function Manager(name, salary, year, month, day, bonus) { | |
var _this = this; | |
Employee.call(_this, name, salary, year, month, day); | |
_this.post = "Manager"; | |
_this.bonus = bonus || 0; | |
_managersCount++; | |
} | |
// 在这里实现继承,而不是在函数外部实现继承,思考为什么? | |
inheritPrototype(Manager, Employee); | |
Manager.prototype.setBonus = function (num) { | |
this.bonus = num; | |
}; | |
Manager.getNextId = function() { | |
return Employee.getNextId(); | |
}; | |
Manager.setNextId = function(value) { | |
return Employee.setNextId(value); | |
}; | |
Manager.getCounts = function() { | |
return _managersCount; | |
}; | |
return Manager; | |
})(); | |
// 测试代码 | |
var yangfch3 = new Employee("yangfch3", 1234, 2014, 9, 26); | |
Employee.getCounts(); | |
Employee.getNextId(); | |
Employee.setNextId(1234); | |
Object.keys(yangfch3); | |
yangfch3.getId(); | |
yangfch3.setId(4321); | |
yangfch3.computeSeniority(); | |
yangfch3.bonus; | |
yangfch3.setBonus; | |
var Qu = new Manager("Qu", 12345, 2001, 8, 5, 500); | |
Manager.getCounts(); | |
Manager.getNextId(); | |
Manager.setNextId(1234); | |
Object.keys(Qu); | |
Qu.getId(); | |
Qu.setId(4321); | |
Qu.computeSeniority(); | |
Qu.bonus; | |
Qu.setBonus(1111); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment