前端开发,每天进步一点点,加油!
正文
<script>
//原型定义
//扩展原生对象
//这样做会污染原生对象 Function
Function.prototype.checkEmail = function () {
console.log("验证邮箱");
};
//优化:抽象出一个统一添加方法的功能
Function.prototype.addMethod = function (name, fn) {
this[name] = fn;
};
Function.addMethod("checkName", function () {
console.log("验证姓名");
});
//书写优化
function checkPassword() {
console.log("验证密码");
}
Function.addMethod("checkPassword", checkPassword);
function test() {
console.log("test");
Function.checkEmail();
Function.checkName();
Function.checkPassword();
}
test();
</script>