[关闭]
@GivenCui 2016-08-11T22:38:29.000000Z 字数 2403 阅读 674

第一章--JavaScript灵活的语言

JavaScript设计模式



需求: 验证用户名、邮箱、密码


1. 函数声明与函数表达式

创建了全局变量, 会污染全局

  1. // 1.1 函数声明
  2. function checkName () {
  3. console.log("验证用户名");
  4. }
  5. function checkEmail () {
  6. console.log("验证邮箱");
  7. }
  8. function checkPassword () {
  9. console.log("验证密码");
  10. }
  11. // 1.2 函数表达式
  12. var checkName = function () {
  13. console.log("验证用户名");
  14. };
  15. var checkEmail = function () {
  16. console.log("验证邮箱");
  17. };
  18. var checkPassword = function () {
  19. console.log("验证密码");
  20. };

2. 用对象收编

只是一个单例, 不能复制多份

  1. // 变为对象中的方法
  2. var checkObject = {
  3. checkName : function () {
  4. console.log("验证用户名");
  5. },
  6. checkEmail : function () {
  7. console.log("验证邮箱");
  8. },
  9. checkPassword : function () {
  10. console.log("验证密码");
  11. }
  12. };
  1. // 变为函数(特殊的对象)的方法
  2. var checkObjec = function () {};
  3. checkObject.checkName = function () {
  4. console.log("验证用户名");
  5. };
  6. checkObject.checkEmail = function () {
  7. console.log("验证邮箱");
  8. };
  9. checkObject.checkPassword = function () {
  10. console.log("验证密码");
  11. };

3. 函数调用返回新对象

新对象与函数之间没有关系

  1. var checkObject = function () {
  2. return {
  3. checkName : function () {
  4. console.log("验证用户名");
  5. return this;
  6. },
  7. checkEmail : function () {
  8. console.log("验证邮箱");
  9. return this;
  10. },
  11. checkPassword : function () {
  12. console.log("验证密码");
  13. return this;
  14. }
  15. };
  16. };
  17. // 调用
  18. var a = checkObject();
  19. a.checkName().checkEmail().checkPassword();

4. 用构造函数

比较完美的方案

  1. // 构造函数
  2. var CheckObject = function () {};
  3. CheckObject.prototype = {
  4. checkName : function () {
  5. console.log("验证用户名");
  6. return this;
  7. },
  8. checkEmail : function () {
  9. console.log("验证邮箱");
  10. return this;
  11. },
  12. checkPassword : function () {
  13. console.log("验证密码");
  14. return this;
  15. }
  16. };
  17. // 调用
  18. var a = new CheckObject();
  19. a.checkName().checkEmail().checkPassword();

5. 向Function的prototype中拓展插件

不能直接向Funtction的原型中添加方法, 会污染
编写一个插件管理器

  1. // 函数式调用方法
  2. // 插件管理器
  3. Function.prototype.addMethod = function (name, fn) {
  4. this[name] = fn; // 在函数上调用
  5. return this;
  6. };
  7. var m1 = function () {}; // function是Function的实例
  8. m1.addMethod("checkName", function () {
  9. console.log("验证用户名");
  10. return this;
  11. }).addMethod("checkEmail", function () {
  12. console.log("验证邮箱");
  13. return this;
  14. }).addMethod("checkPassword", function () {
  15. console.log("验证密码");
  16. return this;
  17. });
  18. // 调用
  19. m1.checkName().checkEmail().checkPassword();
  1. // 类式调用方法
  2. // 插件内部实现不同
  3. Function.prototype.addMethod = function (name,fn) {
  4. this.prototype[name] = fn; // 添加在了原型上,所有类式调用
  5. return this;
  6. };
  7. var M1 = function () {}; // function是Function的实例
  8. M1.addMethod("checkName", function () {
  9. console.log("验证用户名");
  10. return this;
  11. }).addMethod("checkEmail", function () {
  12. console.log("验证邮箱");
  13. return this;
  14. }).addMethod("checkPassword", function () {
  15. console.log("验证密码");
  16. return this;
  17. });
  18. // 调用
  19. var a = new M1();
  20. a.checkName().checkEmail().checkPassword();
  21. var b = new M1();
  22. b.checkName().checkEmail().checkPassword();
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注