javascript设计模式系列一
1、javascript灵活语言
方式一、
function checkName() { } function checkEmail()) { } function checkPassword() { }
方式二、
var checkName=function(){} var checkEmail=function(){} var checkPasswork=function(){}
方式1一和方式二都是全局作用域变量,方式二能直观说名在javascript中函数也是变量,只是定义时位置不同而已
2、用对象收编变量
方式一、
var checkObject={ checkName:function(){}, checkEmail:function(){}, checkPassword:function(){} }
方式二、
var checkObject = function () { } checkObject.checkName = function () { return this; } checkObject.checkEmail = function () { return this; } checkObject.checkPassword = function () { return this; }
3、真假对象
var checkObject = function () { return { checkName: function () { }, checkEmail: function () { }, checkPassword: function () { } } }
var obj= checkObject();
obj.checkName();
4、类形式
var checkObject = function () { this.checkName = function () { }; this.checkEmail = function(){ }; this.checkPassword = function(){ }; } var obj = new checkObject(); obj.checkName();
5、原型形式
方式一、
var checkObject = function () { } checkObject.checkName = function () { } checkObject.checkEmail = function () { } checkObject.checkPassword = function(){ }
方式二、
var checkObject = function () { return { checkName: function () { }, checkEmail: function () { }, checkPassword: function () { } } }
6、原型形式加 Return
var checkObject = function () { } checkObject.checkName = function () { return this; } checkObject.checkEmail = function () { return this; } checkObject.checkPassword = function(){ return this; }
方式二、
var checkObject = function () { return { checkName: function () { return this; }, checkEmail: function () { return this; }, checkPassword: function () { return this; } } }
生命不息,奋斗不止