diff --git a/README.txt b/README.txt index eaa0d12..ca57cff 100644 --- a/README.txt +++ b/README.txt @@ -14,4 +14,5 @@ and at the Apress website: http://apress.com/book/view/159059908x Questions and corrections can be sent to ross@jsdesignpatterns.com and -dustin@jsdesignpatterns.com. \ No newline at end of file +dustin@jsdesignpatterns.com. +right \ No newline at end of file diff --git a/javascript/DateString.js b/javascript/DateString.js new file mode 100644 index 0000000..f2bd904 --- /dev/null +++ b/javascript/DateString.js @@ -0,0 +1,17 @@ +String.prototype.dateStringToDate=function(text){ + console.log(this); +} +Date.prototype.toMyDateString=function(){ + var year=this.getFullYear(); + var month=this.getMonth(); + var date=this.getDate(); + + return year+"-"+month+"-"+date; +} + +var s="I am fudongdong"; + +s.dateStringToDate(); + +var d=new Date(); +console.log(d.toMyDateString()); \ No newline at end of file diff --git a/javascript/compare.js b/javascript/compare.js new file mode 100644 index 0000000..ddf2761 --- /dev/null +++ b/javascript/compare.js @@ -0,0 +1,14 @@ +function createCompareFun(name){ + return function(obj1,obj2){ + var one=obj1[name]; + var two=obj2[name]; + + if(one0){ +// status.required=false; +// } +// }else if(field.is(type[2])){ +// if(field.is("[name]")){ +// if($("[name='"+field.prop("name")+"']:checked").length == 0){ +// status.required=false; +// } +// }else{ +// status.required=field.is(":checked"); +// } +// } +// } + +// if(field.is(type[0])){ +// if(fieldPattern.test(fieldValue)){ +// if(event.type != "keyup" && fieldMask !== undefined){ +// var matches=fieldValue.match(fieldPattern); + +// for(var i=0,len=matches.length;i 0){ +// status.pattern=false; +// } +// } +// } +// } + +// var describedby=$('[id="'+fieldDescribedby+'"]'); +// var log=fieldDescription.valid; + +// if(descrribedby.length> 0 && event.type !="keyup"){ +// if(!status.required){ +// log=fieldDescription.required; +// }else if(!status.pattern){ +// log=fieldDescription.pattern; +// }else if(!status.conditional){ +// log=fieldDescription.conditioinal; +// } + +// describedby.html(log || ""); +// } + +// if(typeof (validationi.each) == "function"){ +// validation.each.call(field,event,status,options); +// } + +// options.eachField.call(field,event,status,options); + +// if(status.required && status.pattern && status.conditional){ +// if(!!options.waiAria){ +// field.prop("aria-invalide",false); +// } + +// if(typeof(validatin.valid) == "function"){ +// validation.valid.call(field,event,status,options); +// } + +// options.eachInvalidField.call(field,event,status,options); +// } + +// return status; + + + +// } + + +// $.extend({ +// validateExtend:function(options){ +// return $.extend(extend,options); +// }, +// validateSet:function(options){ +// return $.extend(defaults,options); +// } +// }).fn.extend({ +// validate:function(options){ +// options=$.extend({},defaults,optioins); +// return $(this).validateDestroy().each(function(){ +// var form=$(this); +// if(form.is("form")){ +// form.data(name,{ +// optioins:options +// }); + +// var fields=form.find(allTypes); +// var namespace=options.namespace; + + +// if(form.is("[id]")){ +// fields=fields.add('[form="'+form.prop("id")+'"]').filter(allTypes); + +// } + +// fields=fields.filter(options.fillter); + +// if(!!options.onkeyup){ +// fields.filter(type[0])..on("keyup."+namespace,function(event){ +// validateField.call(this,event,options); +// }) +// } + +// if(!!options.onBlur){ +// fields.on("blur."+namespace,function(event){ +// validateField.call(this,event,options); +// }) +// } + +// if(!!options.onChange){ +// fields.on("change."+namespace,function(event){ +// validateField.call(this,event,options); +// }) +// } + +// if(!!optioins.onSubmit){ +// form.on("submit."+namespace,function(event){ +// var formValid=true; +// fields.each(function(){ +// var status=validateField.call(this,event,options) +// if(!status.pattern || !status.conditional || status.required){ +// formValid=false; +// } +// }) + +// if(formValid){ +// if(!options.sendForm){ +// event.preventDefault(); +// } + +// if($.isFunction(options.valid)){ +// options.invalid.call(form,event,options); +// } +// } +// }); +// } + + +// } +// }) +// }, +// validateDestroy:function(){ +// var form=$(this); +// var dataValidate=form.data(name); + +// if(form.is("form") && $.isPlainObject(dataBalidate) && typeof(dataBalidate.options.nameSpace) == "string"){ +// var fields=form.removeData(name).find(allTypes).add(fomr); +// if(form.is("[id]")){ +// fields=fields.add($('[form="'+form.prop("id")+'"]').filter(allTypes)); + +// } + +// fields.off("."+dataValidate.options.nameSpace); +// } + +// return false; +// } +// }) + + + +// })({ +// sendForm:true, +// waiAria:true,//use WAI-ARIA properties +// onSubmit:true, +// onKeyup:false, +// onBlur:false, +// onChange:false, +// nameSpace:"validate", +// conditional:{}, +// prepare:{}, +// description:{}, +// eachField:$.noop, +// eachInvalidField:$.noop, +// eachValidField:$.noop, +// invalid:$.noop, +// valid:$.noop, +// fiter:"*" +// },jQuery,window); + +;(function(defaults,,ドルwindow,undefined){ + + var type=['input:not([type]),input[type="color"],input[type="date"],input[type="datetime"],input[type="datetime-local"],input[type="email"],input[type="file"],input[type="hidden"],input[type="month"],input[type="number"],input[type="password"],input[type="range"],input[type="search"],input[type="tel"],input[type="text"],input[type="time"],input[type="url"],input[type="week"],textarea', 'select', 'input[type="checkbox"],input[type="radio"]']; + var allTypes=type.join(","); + var extend={}; + + validateField=function(){ + //最终返回status,其属性分别代表field验证结果 + var status={ + pattern:true, + conditional:true, + required:true + }; + + + var field=$(this); + var fieldValue=field.val() || ""; + var fieldValidate=field.data("validate"); + var validation=fieldValidate !==undefined ? extend[fieldValidate] :{}; + + var fieldPrepare=field.data("prepare") || validation.prepare; + + var fieldPatern=(field.data("pattern") || ($.type(validation.pattern) == "regexp" ? validation.pattern:/(?:)/)); + var fieldIgnoreCase = field.attr("data-ignore-case") || field.data("ignoreCase") || validation.ignoreCase; + var fieldMask=field.data("mask") || validation.mask; + var fieldConditional=field.data("conditional") || validation.conditional; + var fieldRequired=field.data("required"); + var fieldDescribedby=field.data("description") || validatation.description; + var fieldDescription=field.data("description") || validation.description; + var fieldTrim=field.data("trim"); + + var reTrue=/^true$/i; + var reFalse=/^false$/i; + + fieldDescription=fieldDescription; + var name="validate"; + fieldRequired=fieldRequired; + fieldTrim=fieldTrim; + + if(fieldTrim){ + fieldValue=$.trim(fieldValue); + } + if($.isFunction(fieldPrepare)){ + fieldValue=String(fieldPrepare.call(field,fieldValue)); + }else if($.isFunction(options.prepare[fieldPrepare])){ + fieldValue=String(options.prepare[fieldPrepare].call(field,fieldValue)); + } + + if($.type(fieldPattern) != "regexp"){ + fieldPattern=fieldIgnoreCase ? RegExp(fieldPattern,"i") : RegExp("fieldPatern"); + } + + if(fieldConditional != undefined){ + if($.isFunction(fieldConditional)){ + status.conditional= !!fieldConditional.call(field,fieldValue,options); + + + }else{ + var conditionals=fieldConditional.split(/[\s\t]/); + for(var counter=conditional.length;counter 0){ + status.required=false; + } + }else if(field.is(type[2])){ + if(field.is('[name]')){ + if($'[name='+field.prop("name")+'"]:checked').length == 0){ + status.required=false; + } + }else{ + status.required=field.is(":checked"); + } + } + } + + + + + + + + + + return status; + } + + +})({ + sendForm:true, + waiAria:true, + onSubmit:true, + onKeyup:false, + onBlur:false, + onChange:false, + nameSpace:"validate", + conditional:{}, + prepare:{}, + description:{}, + eachField:$.noop + eachInvalidField:$.noop, + eachValidField:$.noop, + invalid:$.noop, + valid:$.noop + +},jQuery,window) \ No newline at end of file diff --git a/javascript/one.js b/javascript/one.js new file mode 100644 index 0000000..9ba54ca --- /dev/null +++ b/javascript/one.js @@ -0,0 +1,23 @@ +var book={ + _year:2004, + //下划线表示只能通过对象方法访问 + edition:1 +} + +Object.defineProperty(book,"year",{ + get:function(){ + return this._year; + }, + set:function(newValue){ + if(newValue>2004){ + this._year=newValue; + this.edition=newValue-2003; + }else{ + throw new Error("年份错误,必须大于2004"); + } + } +}) + +book.year=2008; +console.log(book.edition); + diff --git a/javascript/two.js b/javascript/two.js new file mode 100644 index 0000000..e7eb414 --- /dev/null +++ b/javascript/two.js @@ -0,0 +1,37 @@ +var book={}; + +Object.defineProperties(book,{ + _year:{ + value:2004, + writable:true, + configurable:true + }, + edition:{ + value:1, + writable:true, + configurable:true + }, + year:{ + get:function(){ + return this._year; + }, + set:function(newValue){ + if(newValue>2004){ + this._year=newValue; + this.edition=newValue-2003; + }else{ + throw new Error("年份不能小于2004"); + } + } + } +}) + + +console.log(book.year); +book.year=2013; + +console.log(book.year); +console.log(book.edition); + + +book.year=1030; \ No newline at end of file diff --git a/object-oriented/Inheritance/3 - The extend function.js b/object-oriented/Inheritance/3 - The extend function.js index f92379f..0b6a237 100644 --- a/object-oriented/Inheritance/3 - The extend function.js +++ b/object-oriented/Inheritance/3 - The extend function.js @@ -1,10 +1,12 @@ /* Extend function. */ function extend(subClass, superClass) { - var F = function() {}; - F.prototype = superClass.prototype; - subClass.prototype = new F(); - subClass.prototype.constructor = subClass; + // var F = function() {}; + // F.prototype = superClass.prototype; + // subClass.prototype = new F(); + // subClass.prototype.constructor = subClass; + subClass.prototype=superClass.prototype; + subClass.constructor=subClass; } @@ -21,7 +23,7 @@ Person.prototype.getName = function() { /* Class Author. */ function Author(name, books) { - Person.call(this, name); + Person.call(this, name);//Author继承了Person的属性 相当于执行了this.name=name this.books = books; } extend(Author, Person); @@ -31,36 +33,8 @@ Author.prototype.getBooks = function() { }; +var fdd=new Author("fdd","javascript"); +console.log(fdd.getName()); +console.log(fdd.getBooks()); -/* Extend function, improved. */ -function extend(subClass, superClass) { - var F = function() {}; - F.prototype = superClass.prototype; - subClass.prototype = new F(); - subClass.prototype.constructor = subClass; - - // The second way, to add the below code - subClass.superclass = superClass.prototype; - if(superClass.prototype.constructor == Object.prototype.constructor) { - superClass.prototype.constructor = superClass; - } -} - - -/* Class Author. */ - -function Author(name, books) { - Author.superclass.constructor.call(this, name); - this.books = books; -} -extend(Author, Person); - -Author.prototype.getBooks = function() { - return this.books; -}; - -Author.prototype.getName = function() { - var name = Author.superclass.getName.call(this); // The second way can access superClass method like this - return name + ', Author of ' + this.getBooks().join(', '); -}; diff --git a/object-oriented/Inheritance/6 - The clone function.js b/object-oriented/Inheritance/6 - The clone function.js index 867c65c..10500cc 100644 --- a/object-oriented/Inheritance/6 - The clone function.js +++ b/object-oriented/Inheritance/6 - The clone function.js @@ -1,7 +1,55 @@ /* Clone function. */ -function clone(object) { - function F() {} - F.prototype = object; - return new F; +// function clone(object) { +// function F() {} +// F.prototype = object; +// return new F; +// } + + +function clone(obj){ + var o={}; + if(typeof obj =="object"){ + if(obj === null){ + o=null; + }else{ + if(obj instanceof Array){ + for(var i=0,len=obj.length;i

AltStyle によって変換されたページ (->オリジナル) /