From 33194d3ab74a0156a655912fd0feecbcd4a7733e Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 21 Jun 2017 10:20:57 +0800 Subject: [PATCH 01/50] update --- .DS_Store | Bin 8196 -> 10244 bytes base/base.js | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 base/base.js diff --git a/.DS_Store b/.DS_Store index b1f593066eac1da8841b10efeaf8d3f79dff14f9..01d221da77ad1aa00659def850006712f6b9cded 100644 GIT binary patch delta 346 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50C@KczFar4uo;mr+NjdpRAjyq| z)7U3A@NQ=3VBs(YsbFD9Vn}2tW=KU=#lXO136wJc;`;ypfh3Z;$@fKf30Yd|C>WcY z)#@lzTN;__D3}^gUM*(B4mE7@dJ)k~xWT#kE-ophCCLm7SM%GCpc? zIX|}mtOG*8%`6Kp%FD^mO9u)wZsrl#!OAGN`KYK9)5ZpSHbG{HVg+s>?FtHzjfLNt lC-ch$iZDTfNdu$+7&IXNF-$Iy>D}xg+`_~tH~FY&9RMKKN67#H delta 133 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aKK$`6tU@);OX8Hzk}@{^Nt@{=|e zPGg_gz`L29gN1{UfAd+v9juJfo7=>lm;{-D>VZIl8%Vf<%-LA@oq009N+1W&6p-l* RljC`&P2Mkd46MVR2>@Zh8h!u( diff --git a/base/base.js b/base/base.js new file mode 100644 index 0000000..e3c330b --- /dev/null +++ b/base/base.js @@ -0,0 +1,22 @@ +;(function(window, document, undefined) { + + function Base() {} + + Base.prototype.queryParams = function() { + + }; + + var base = new Base(); + + window.base = base; + +})(window, document); + +if (typeof module !== 'undefined') { + module.exports = window.Ajax; +} else if (typeof define === 'function' && define.amd) { + define([], function () { + 'use strict'; + return window.Ajax; + }); +} \ No newline at end of file From 6022d0b3f08d12fed76d74ff68d9c10273048946 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 21 Jun 2017 10:26:46 +0800 Subject: [PATCH 02/50] update --- base/base.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/base/base.js b/base/base.js index e3c330b..f2f370c 100644 --- a/base/base.js +++ b/base/base.js @@ -2,8 +2,31 @@ function Base() {} - Base.prototype.queryParams = function() { + Base.prototype.server = function(Ajax, data, url, callback, type, dataType) { //Server方法 + Ajax({ + data: data || {}, + type: type || 'get', + url: url, + dataType: dataType || 'json', + done: (res) => { + callback && callback(res); + } + }); + }; + Base.prototype.queryParams = function() { //获取查询字符串参数 + var search = location.search, + theRequest = {}; + if (search.indexOf('?') < 0) { + return; + } + search = search.substr(1); + var paramArr = search.split('&'), + max = paramArr.length; + for (var i = 0; i < max; i ++) { + theRequest[paramArr[i].split('=')[0]] = unescape(paramArr[i].split('=')[1]); + } + return theRequest; }; var base = new Base(); @@ -13,10 +36,10 @@ })(window, document); if (typeof module !== 'undefined') { - module.exports = window.Ajax; + module.exports = window.base; } else if (typeof define === 'function' && define.amd) { define([], function () { 'use strict'; - return window.Ajax; + return window.base; }); } \ No newline at end of file From cb0c3f70374a3fe148c22b5d7b8a09184dbec577 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 21 Jun 2017 10:30:39 +0800 Subject: [PATCH 03/50] update --- base/base.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/base/base.js b/base/base.js index f2f370c..593517b 100644 --- a/base/base.js +++ b/base/base.js @@ -29,17 +29,35 @@ return theRequest; }; - var base = new Base(); + Base.prototype.checkPlatforms = function() { + var u = navigator.userAgent, app = navigator.appVersion; +        return { +            trident: u.indexOf('Trident') > -1, //IE内核 +            presto: u.indexOf('Presto') > -1, //opera内核 +            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 +            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核 +            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 +            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 +            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 +            iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器 +            iPad: u.indexOf('iPad') > -1, //是否iPad +            webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 +            weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增) +            qq: u.match(/\sQQ/i) == " qq" //是否QQ +        }; + }; + + var basefn = new Base(); - window.base = base; + window.basefn = basefn; })(window, document); if (typeof module !== 'undefined') { - module.exports = window.base; + module.exports = window.basefn; } else if (typeof define === 'function' && define.amd) { define([], function () { 'use strict'; - return window.base; + return window.basefn; }); } \ No newline at end of file From 34a63fafa3f63ed99c280b35e70da637800be9df Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 21 Jun 2017 14:32:27 +0800 Subject: [PATCH 04/50] update --- base/base.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/base/base.js b/base/base.js index 593517b..6c70e58 100644 --- a/base/base.js +++ b/base/base.js @@ -2,16 +2,21 @@ function Base() {} - Base.prototype.server = function(Ajax, data, url, callback, type, dataType) { //Server方法 - Ajax({ - data: data || {}, - type: type || 'get', - url: url, - dataType: dataType || 'json', - done: (res) => { - callback && callback(res); - } - }); + Base.prototype.server = function(Ajax) { //Server方法 + + var Server = Ajax; + + return function(data, url, callback, type, dataType) { + Server({ + data: data || {}, + type: type || 'get', + url: url, + dataType: dataType || 'json', + done: (res) => { + callback && callback(res); + } + }); + } }; Base.prototype.queryParams = function() { //获取查询字符串参数 From 62a6b3afb481bf8c075efea6613c6d48b68235cb Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Mon, 24 Jul 2017 17:05:51 +0800 Subject: [PATCH 05/50] update --- .DS_Store | Bin 10244 -> 10244 bytes base/.DS_Store | Bin 0 -> 6148 bytes base/README.md | 29 +++++++++++++ base/base.js | 25 +++++++++++ base/server.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 base/.DS_Store create mode 100644 base/README.md create mode 100644 base/server.js diff --git a/.DS_Store b/.DS_Store index 01d221da77ad1aa00659def850006712f6b9cded..0cde177c987ff517fba1fef94e3086d6aa05bfcb 100644 GIT binary patch delta 185 zcmZn(XbIS$FTmJ1*+4*=@z-P@fmH^Q)zv1(COQg6mKL=-3e|=N=9W4NW=4j!wE}G% z9MYPmj=qrvwOxxAFIl>5`HCaQCdZ4&b0245U|?ZLVn}2to?IX%x;aJgJ0o+$|NoO4 j#AGH{33F3mHskTh3BppFXNu%-Z)R8c#j^Ro2s1MPoVh)= delta 75 zcmZn(XbIS$FThwo*+4*gvZ&B{Axldg1!HrwS{;RIOCwVq1yke6c@j38Jq3lCm}UO| fpCm3bxmuVTsPymV7LjJ|&8!N4ST-|@GBX1JKj0QM diff --git a/base/.DS_Store b/base/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..33591a5334f1cbf7d15c08df2616bb4e096c87b5 GIT binary patch literal 6148 zcmeHKO-lno41Ljp6)!z{oMTS~|G`?ef(I}459nGirBblyd48b(X?^*ySlz24l0fp( zOfs|cpfeKyGJjcJ022UXs-mbfAQ~PVI*R0DqSzcWtg*Y^Y#+MKz(jvBq~Csm4)@sM z5ud()+i^r`xuQ;~()YB$DVA8^5-o0sGdyACJKk5kF>BhJM1{`%j90S*Ysm6Tg_WgA z29kkfAQ?yoeuM$u*(&2R$F#{nGLQ`H8PM~gP!*fN>S#v?jkN&85yK|*_0g;~C9nys zjvS$gmlC~HqQwv|XS_sS6IdO+91_ik#L6Gdi`eR%zgRk?aZH;GBm;*GXzxpz=>5Oo zUuLk$x0E!=Kr--O8IVzX+s?VWxLd#NQ}5bBy`!pVUR8%e|KJh8A9{|Q+n|pZ^_kZM WR!5yh<5o`0hky~1CK>nz2HpYQi7++* literal 0 HcmV?d00001 diff --git a/base/README.md b/base/README.md new file mode 100644 index 0000000..d064667 --- /dev/null +++ b/base/README.md @@ -0,0 +1,29 @@ +### Usage +``` +//init + +or +import Ajax from './server.js'; + +//use +Ajax({ + data: data, //request data + url: url, //request url + dataType: 'jsonp', //jsonp or not + type: 'get', //request method, support 'get' and 'post' + headers: { + header1: 'aaa', + header2: 'bbb' + }, + success: function(json) { + console.log(json); + }, + fail: function(json) { + console.log(json); + }, + done: function(json) { + console.log(json); + } +}); + +``` diff --git a/base/base.js b/base/base.js index 6c70e58..ff91cd4 100644 --- a/base/base.js +++ b/base/base.js @@ -52,6 +52,31 @@         }; }; + Base.prototype.setCookie = function(name, value, days) { + let Days = days || 1, + exp = new Date(); + exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); + document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString(); + }; + + Base.prototype.getCookie = function(name) { + var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //正则匹配 + if (arr = document.cookie.match(reg)) { + return unescape(arr[2]); + } else { + return null; + } + }; + + Base.prototype.delCookie = function(name) { + var exp = new Date(); + exp.setTime(exp.getTime() - 1); + var cval = this.getCookie(name); + if (cval != null) { + document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); + } + }; + var basefn = new Base(); window.basefn = basefn; diff --git a/base/server.js b/base/server.js new file mode 100644 index 0000000..ad8dd12 --- /dev/null +++ b/base/server.js @@ -0,0 +1,114 @@ +;(function(window, document, undefined) { + function Ajax(opts) { + + opts.type = opts.type || 'get'; + opts.type = opts.type.toLowerCase(); + opts.dataType = opts.dataType || 'json'; + opts.dataType = opts.dataType.toLowerCase(); + + if (opts.dataType == 'jsonp') { + jsonpRequest(opts); + return; + } + + var xhr = new XMLHttpRequest(), + params = null; + + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + opts.done && opts.done(jsonParse(xhr.responseText), xhr.responseXML); + if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { + opts.success && opts.success(jsonParse(xhr.responseText), xhr.responseXML); + } else { + opts.fail && opts.fail(jsonParse(xhr.responseText), xhr.responseXML); + } + } + } + + if (opts.type == 'get') { + params = formatParams(opts.data); + var url = opts.url.indexOf('?') > -1 ? (opts.url + '&' + params) : (opts.url + '?' + params); + xhr.open('get', url, true); + opts.headers && setHeaders(); + xhr.send(null); + } else if (opts.type == 'post') { + params = formatParams(opts.data); + xhr.open('post', opts.url, true); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + opts.headers && setHeaders(); + xhr.send(params); + } + + function formatParams(data) { + var arr = []; + for (var key in data) { + arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); + } + return arr.join('&'); + } + + function jsonpRequest(opts) { + + if (!opts.url) { + console.error('url missing'); + return; + } + + opts.jsonpCallback = opts.jsonpCallback || 'callback'; + + var callbackName = 'jsonp_' + Math.ceil((Math.random() * 1E12)); + + opts.data[opts.jsonpCallback] = callbackName; + + //创建script标签 + var params = formatParams(opts.data), + oHead = document.querySelector('head'), + oScript = document.createElement('script'); + oHead.appendChild(oScript); + + //创建回调函数 + window[callbackName] = function(json) { + oHead.removeChild(oScript); + window[callbackName] = null; + window.clearTimeout(oScript.timer); + opts.success && opts.success(json); + opts.done && opts.done(json); + }; + + //发起请求 + oScript.src = opts.url + '?' + params; + + if (opts.time) { + oScript.timer = window.setTimeout(function() { + oHead.removeChild(oScript); + window[callbackName] = null; + opts.fail && opts.fail({message: 'timeout'}); + opts.done && opts.done({message: 'timeout'}); + }, opts.time); + } + } + + function setHeaders() { + for (var o in opts.headers) { + // console.log(o, opts.headers[o]); + xhr.setRequestHeader(o, opts.headers[o]); + } + } + + function jsonParse(text) { + return JSON.parse(text); + } + } + + window.Ajax = Ajax; + +})(window, document); + +if (typeof module !== 'undefined') { + module.exports = window.Ajax; +} else if (typeof define === 'function' && define.amd) { + define([], function () { + 'use strict'; + return window.Ajax; + }); +} From e7959f5103aed7082ddd6e2b8a4d1a10135d969d Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 22 Nov 2017 14:26:15 +0800 Subject: [PATCH 06/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/inherit.html | 4 ++++ cutter/.DS_Store | Bin 6148 -> 6148 bytes 3 files changed, 4 insertions(+) create mode 100644 OO/inherit.html diff --git a/.DS_Store b/.DS_Store index 0cde177c987ff517fba1fef94e3086d6aa05bfcb..16d134a81ecb58f497fe3ef68852ba4571c014cb 100644 GIT binary patch delta 816 zcmZn(XbG6$&nUYwU^hRb>|`DRdx?`t1v#0;B?bo98JU<_SlQS)I5{~uc{wH<3dv9Y zB5ocN9h{M09*|g)k(5{jmWvmVsIE4#w9rv7HZZ8wQK+^wHqcQpHL=Hw?Q<>V&;T_*s< zK0rM6KNx`B3s#dvYez9y$5zB#^SST delta 70 zcmV-M0J;B!P=rvBPXQLOP`eKS7LyDRKa(XB{s#mAVgO+PbCVGk6_Yd(u#>?U{j;$W c83eOG7{~{)fD{3<2O# + + + \ No newline at end of file diff --git a/cutter/.DS_Store b/cutter/.DS_Store index a24bea50fb0b055015885ccc92da7f9f19e21a44..965ccbd0a2dd51bf4e412e2ad37249bcf0ed7ca0 100644 GIT binary patch delta 146 zcmZoMXffCj%p!3zsURn_xWvHVIwKP^3o9Et2PY>7Cojk3d=~l1fo$e6(ZLz{YCcRnG9gS b$Oxes_@Ok6nzh-Nbr;jb27}G)9Dn%%H&7st delta 34 qcmZoMXffCj%rf~F>pBioV-p<(BNNlf6WH80E3j^7+RV=JmmdJdhYIQd From 0f4c54496a6f3da2eb33c96c71c08b8bbd0e25f6 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 22 Nov 2017 15:17:05 +0800 Subject: [PATCH 07/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/inherit.html | 8 ++++++++ 2 files changed, 8 insertions(+) diff --git a/.DS_Store b/.DS_Store index 16d134a81ecb58f497fe3ef68852ba4571c014cb..4e3fa1d2a1adc9a1788ef2cb6bfd95e7fb417c1f 100644 GIT binary patch delta 42 ncmZn(XbITRB+g-BVXC8GY+*jRK*(|PY4Pp+C?b + + 面向对象的继承 + + + + \ No newline at end of file From 67fff295207af0d768c7894e0b66700309de491d Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 22 Nov 2017 15:21:32 +0800 Subject: [PATCH 08/50] update --- OO/.DS_Store | Bin 0 -> 6148 bytes OO/inherit.html | 12 ++++++++++++ 2 files changed, 12 insertions(+) create mode 100644 OO/.DS_Store diff --git a/OO/.DS_Store b/OO/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7a9dcb565cfb44688e4e81ee092820cd13ca1465 GIT binary patch literal 6148 zcmeHKu}TC{3{3Qj11+w!d|x0~+dPi5u=WS!R8UwwIJDo&+E4P&bS4SHvIrvB$OMvk zFPqu7JH=*WX1;#8Ju_RG*%D5)SBAOqI(=pbm2n^(_t@KmJ=>|>BmT_DJqGIuoVyAC z(T2-mce5RKTh#CY&*5($VOYtefE17dQa}nwf!`>=-V0kEB`Qh*DIf*D6yW!v!HK //继承 +//原型链 + +//借用构造函数 + +//组合继承 + +//原型式继承 + +//寄生式继承 + +//寄生组合式继承 + \ No newline at end of file From 88f300b3808f93fb8f27be82b0cae1f1b116937b Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 22 Nov 2017 15:43:45 +0800 Subject: [PATCH 09/50] update --- OO/.DS_Store | Bin 6148 -> 6148 bytes OO/inherit.html | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/OO/.DS_Store b/OO/.DS_Store index 7a9dcb565cfb44688e4e81ee092820cd13ca1465..9062d55bb70cae760bf078aa7f130fd4f86b049a 100644 GIT binary patch delta 12 TcmZoMXfc?uo6&sZ9uIK Date: Wed, 22 Nov 2017 15:50:21 +0800 Subject: [PATCH 10/50] update --- OO/inherit.html | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/OO/inherit.html b/OO/inherit.html index 58c5f11..8f7b72e 100644 --- a/OO/inherit.html +++ b/OO/inherit.html @@ -7,29 +7,47 @@ From 7884158102211c8bcdb7cd454b6d42ae00611c64 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 22 Nov 2017 17:53:29 +0800 Subject: [PATCH 17/50] update --- OO/inherit.html | 101 +++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/OO/inherit.html b/OO/inherit.html index 98d87b9..3a7d344 100644 --- a/OO/inherit.html +++ b/OO/inherit.html @@ -73,40 +73,43 @@ //【组合继承(伪经典继承)】 -// function SuperType(name) { -// this.name = name; -// this.colors = ['red', 'blue', 'green']; -// } +function SuperType(name) { + this.name = name; + this.colors = ['red', 'blue', 'green']; +} -// SuperType.prototype.sayName = function() { -// console.log(this.name); -// }; +SuperType.prototype.sayName = function() { + console.log(this.name); +}; -// function SubType(name, age) { -// SuperType.call(this, name); -// this.age = age; -// } +function SubType(name, age) { + SuperType.call(this, name); //第二次调用 SuperType() + this.age = age; +} -// SubType.prototype = new SuperType(); +SubType.prototype = new SuperType(); //第一次调用 SuperType() // console.log(SubType.prototype.constructor); //SuperType -// SubType.prototype.constructor = SubType; -// SubType.prototype.sayAge = function() { -// console.log(this.age); -// }; +SubType.prototype.constructor = SubType; +SubType.prototype.sayAge = function() { + console.log(this.age); +}; -// var instanceA = new SubType('AAA', 29); -// instanceA.colors.push('black'); -// console.log(instanceA.colors); -// instanceA.sayName(); -// instanceA.sayAge(); +var instanceA = new SubType('AAA', 29); +instanceA.colors.push('black'); +console.log(instanceA.colors); +instanceA.sayName(); +instanceA.sayAge(); -// var instanceB = new SubType('BBB', 27); -// console.log(instanceB.colors); -// instanceB.sayName(); -// instanceB.sayAge(); +var instanceB = new SubType('BBB', 27); +console.log(instanceB.colors); +instanceB.sayName(); +instanceB.sayAge(); //组合继承问题 //组合继承融合了原型链与借用构造函数的优点,避免了二者的缺陷,是最常用的继承模式。 +//由于调用了两次 SuperType,会分别在 SubType 的原型对象和实例对象上分别创建属性。 + +console.log(instanceA); //instanceA.name & instanceA.__proto__.name //【组合继承】 @@ -178,31 +181,39 @@ //【寄生式继承】 //【寄生组合式继承】 -function inheritPrototype(subType, superType) { - var prototype = Object.create(superType.prototype); - prototype.constructor = subType; - subType.prototype = prototype; -} +// function inheritPrototype(subType, superType) { +// var prototype = Object.create(superType.prototype); +// prototype.constructor = subType; +// subType.prototype = prototype; +// } -function SuperType(name) { - this.name = name; - this.colors = ['red', 'blue', 'green']; -} +// function SuperType(name) { +// this.name = name; +// this.colors = ['red', 'blue', 'green']; +// } -SuperType.prototype.sayName = function() { - console.log(this.name); -}; +// SuperType.prototype.sayName = function() { +// console.log(this.name); +// }; -function SubType(name, age) { - SubType.call(this, name); - this.age = age; -} +// function SubType(name, age) { +// SuperType.call(this, name); +// this.age = age; +// } -inheritPrototype(SubType, SuperType); +// inheritPrototype(SubType, SuperType); + +// SubType.prototype.sayAge = function() { +// console.log(this.age); +// }; + +// var instanceA = new SubType('aaa', 26); + +// console.log(instanceA); + +//寄生组合式继承问题 +//上例的高效率体现在它只调用了一次 SuperType 构造函数,并且因此避免了在 SubType.prototype 上创建不必要的属性。寄生组合式继承是引用类型最理想的继承范式。 -SubType.prototype.sayAge = function() { - console.log(this.age); -}; //【寄生组合式继承】 From f827dbfe6b1e03af123fd6987e658c6a04e04c58 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 09:41:41 +0800 Subject: [PATCH 18/50] update --- OO/createObject.html | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 OO/createObject.html diff --git a/OO/createObject.html b/OO/createObject.html new file mode 100644 index 0000000..27061e2 --- /dev/null +++ b/OO/createObject.html @@ -0,0 +1,10 @@ + + + + 创建对象 + + + + + \ No newline at end of file From e73cca071b918b2c2904f332dd77a3589b08d02e Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 10:21:42 +0800 Subject: [PATCH 19/50] update --- OO/createObject.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/OO/createObject.html b/OO/createObject.html index 27061e2..e088096 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -5,6 +5,26 @@ \ No newline at end of file From efc76474f57415799e6f8d061eb9139f2e9e564e Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 10:33:00 +0800 Subject: [PATCH 20/50] update --- OO/createObject.html | 1 + 1 file changed, 1 insertion(+) diff --git a/OO/createObject.html b/OO/createObject.html index e088096..d6860b2 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -25,6 +25,7 @@ //【寄生模式】 //【寄生模式】 + \ No newline at end of file From 773b722744dbc78c1f3d13c0ebb040b16b1c7c0d Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 11:11:45 +0800 Subject: [PATCH 21/50] update --- OO/createObject.html | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/OO/createObject.html b/OO/createObject.html index d6860b2..d3c7701 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -9,11 +9,45 @@ //【工厂模式】 +function Person(name, age) { + var o = new Object(); + o.name = name; + o.age = age; + o.sayName = function() { + console.log(this.name); + }; + return o; +} + +var person1 = Person('Nicholas', 29), + person2 = Person('Greg', 27); //【工厂模式】 +//【构造函数模式】 +function Person(name, age) { + this.name = name; + this.age = age; + this.sayName = function() { + console.log(this.name); + }; +} + +var person1 = new Person('Nicholas', 29), + person2 = new Person('Greg', 27); +//【构造函数模式】 //【原型模式】 +function Person() {} +Person.prototype.name = 'Nicholas'; +Person.prototype.age = 29; +Person.prototype.sayName = function() { + console.log(this.name); +}; + +var person1 = new Person(); +person1.sayName(); + //【原型模式】 @@ -25,7 +59,6 @@ //【寄生模式】 //【寄生模式】 - \ No newline at end of file From a976d5a184848f4d145543a56d6bfc82a5f05438 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 11:25:24 +0800 Subject: [PATCH 22/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/.DS_Store | Bin 6148 -> 6148 bytes OO/createObject.html | 82 ++++++++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4e3fa1d2a1adc9a1788ef2cb6bfd95e7fb417c1f..16d134a81ecb58f497fe3ef68852ba4571c014cb 100644 GIT binary patch delta 24 dcmZn(XbITRBtH4Pfam7Z;@kN_jLE0PEdhp{3UL4c delta 42 ncmZn(XbITRB+g-BVXC8GY+*jRK*(|PY4Pp+C?bB`mu~2NHo}wrd0|Nsi1A_nqLkUAFgFizOLl#5o#EZ)(8;G!o3o;}# z6aj@3ktG?DC(e=PT?y1@{2vS$7&i9oWZTTn!OsCScjLwH%#-;=ba{XZKqfOZ0P$uY KksZtv8(08)s2|V( delta 87 zcmZoMXfc=|#>CJzu~2NHo}wrt0|NsP3otMgF=R57Fz7L4FqBLzRA*$|?8ms1ZSn%f mLz~$-_&I<&Hcw>y&ODi4M3)n!=>QNjOt#^X-W(&cgc$&2p%eZ9 diff --git a/OO/createObject.html b/OO/createObject.html index d3c7701..593c7ea 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -9,50 +9,76 @@ //【工厂模式】 -function Person(name, age) { - var o = new Object(); - o.name = name; - o.age = age; - o.sayName = function() { - console.log(this.name); - }; - return o; -} +// function Person(name, age) { +// var o = new Object(); +// o.name = name; +// o.age = age; +// o.sayName = function() { +// console.log(this.name); +// }; +// return o; +// } + +// var person1 = Person('Nicholas', 29), +// person2 = Person('Greg', 27); -var person1 = Person('Nicholas', 29), - person2 = Person('Greg', 27); //【工厂模式】 + + //【构造函数模式】 -function Person(name, age) { - this.name = name; - this.age = age; - this.sayName = function() { - console.log(this.name); - }; -} +// function Person(name, age) { +// this.name = name; +// this.age = age; +// this.sayName = function() { +// console.log(this.name); +// }; +// } + +// var person1 = new Person('Nicholas', 29), +// person2 = new Person('Greg', 27); -var person1 = new Person('Nicholas', 29), - person2 = new Person('Greg', 27); //【构造函数模式】 + //【原型模式】 -function Person() {} -Person.prototype.name = 'Nicholas'; -Person.prototype.age = 29; -Person.prototype.sayName = function() { - console.log(this.name); -}; +// function Person() {} +// Person.prototype.name = 'Nicholas'; +// Person.prototype.age = 29; +// Person.prototype.sayName = function() { +// console.log(this.name); +// }; -var person1 = new Person(); -person1.sayName(); +// var person1 = new Person(); +// person1.sayName(); //【原型模式】 //【组合模式】 +function Person(name, age) { + this.name = name; + this.age = age; + this.friends = ['Shelby', 'Court']; +} + +Person.prototype = { + constructor: Person, + sayName: function() { + console.log(this.name); + } +}; + +var person1 = new Person('Nicholas', 29), + person2 = new Person('Greg', 27); + +person1.friends.push('Van'); +console.log(person1.friends); +console.log(person2.friends); +console.log(person1.friends === person2.friends); //false +console.log(person1.sayName === person1.sayName); //true //【组合模式】 From e24b1bb762b6e0a032c562626d1d3a3a6ef3c1c9 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 11:27:16 +0800 Subject: [PATCH 23/50] update --- OO/createObject.html | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 593c7ea..700ca4f 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -83,8 +83,19 @@ -//【寄生模式】 -//【寄生模式】 +//【动态原型模式】 +//【动态原型模式】 + + + +//【寄生构造函数模式】 +//【寄生构造函数模式】 + + + +//【稳妥构造函数模式】 +//【稳妥构造函数模式】 + \ No newline at end of file From 5a56f09be7b4ebfbe815cc23d05e6b9b8d0203fd Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 11:30:17 +0800 Subject: [PATCH 24/50] update --- OO/createObject.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OO/createObject.html b/OO/createObject.html index 700ca4f..be69afa 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -78,7 +78,7 @@ console.log(person1.friends); console.log(person2.friends); console.log(person1.friends === person2.friends); //false -console.log(person1.sayName === person1.sayName); //true +console.log(person1.sayName === person2.sayName); //true //【组合模式】 From 2618a7e044c48fe9e4378afa11c279268ed20b82 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 14:06:16 +0800 Subject: [PATCH 25/50] update --- OO/createObject.html | 66 ++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index be69afa..d2c55c8 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -58,37 +58,61 @@ //【组合模式】 -function Person(name, age) { - this.name = name; - this.age = age; - this.friends = ['Shelby', 'Court']; -} - -Person.prototype = { - constructor: Person, - sayName: function() { - console.log(this.name); - } -}; - -var person1 = new Person('Nicholas', 29), - person2 = new Person('Greg', 27); - -person1.friends.push('Van'); -console.log(person1.friends); -console.log(person2.friends); -console.log(person1.friends === person2.friends); //false -console.log(person1.sayName === person2.sayName); //true +// function Person(name, age) { +// this.name = name; +// this.age = age; +// this.friends = ['Shelby', 'Court']; +// } + +// Person.prototype = { +// constructor: Person, +// sayName: function() { +// console.log(this.name); +// } +// }; + +// var person1 = new Person('Nicholas', 29), +// person2 = new Person('Greg', 27); + +// person1.friends.push('Van'); +// console.log(person1.friends); +// console.log(person2.friends); +// console.log(person1.friends === person2.friends); //false +// console.log(person1.sayName === person2.sayName); //true //【组合模式】 //【动态原型模式】 +// function Person(name, age) { +// this.name = name; +// this.age = age; + +// if (typeof this.sayName != 'function') { +// Person.prototype.sayName = function() { +// console.log(this.name); +// } +// } +// } //【动态原型模式】 //【寄生构造函数模式】 +// function SpecialArray() { +// var values = new Array(); + +// values.push.apply(values, arguments); + +// values.toPipedString = function() { +// return this.join('|'); +// }; + +// return values; +// } + +// var colors = new SpecialArray('red', 'blue', 'green'); +// console.log(colors.toPipedString()); //【寄生构造函数模式】 From d189bf3931e31acdb6687fc133d3617a6f48b06f Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 14:09:03 +0800 Subject: [PATCH 26/50] update --- OO/createObject.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/OO/createObject.html b/OO/createObject.html index d2c55c8..8bd99b0 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -118,6 +118,18 @@ //【稳妥构造函数模式】 +function Person(name, age) { + var o = new Object(); + + o.sayName = function() { + console.log(name); + }; + + return o; +} + +var friend = Person('Nicholas', 29); +friend.sayName(); //【稳妥构造函数模式】 From 1c38e5c00fbef86a87ea34aa25952d352c29610d Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 14:18:03 +0800 Subject: [PATCH 27/50] update --- OO/createObject.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 8bd99b0..05aad59 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -118,18 +118,18 @@ //【稳妥构造函数模式】 -function Person(name, age) { - var o = new Object(); +// function Person(name, age) { +// var o = new Object(); - o.sayName = function() { - console.log(name); - }; +// o.sayName = function() { +// console.log(name); +// }; - return o; -} +// return o; +// } -var friend = Person('Nicholas', 29); -friend.sayName(); +// var friend = Person('Nicholas', 29); +// friend.sayName(); //【稳妥构造函数模式】 From 6affd2faaccb32f06c1eed5898d592fe7eaa7246 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 14:19:23 +0800 Subject: [PATCH 28/50] update --- OO/createObject.html | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 05aad59..741bfda 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -9,19 +9,19 @@ //【工厂模式】 -// function Person(name, age) { -// var o = new Object(); -// o.name = name; -// o.age = age; -// o.sayName = function() { -// console.log(this.name); -// }; -// return o; -// } - -// var person1 = Person('Nicholas', 29), -// person2 = Person('Greg', 27); - +function Person(name, age) { + var o = new Object(); + o.name = name; + o.age = age; + o.sayName = function() { + console.log(this.name); + }; + return o; +} + +var person1 = Person('Nicholas', 29), + person2 = Person('Greg', 27); + //【工厂模式】 From 518b2833d6d57bc5bdde16637cfe7864ffcf3b93 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 14:21:52 +0800 Subject: [PATCH 29/50] update --- OO/createObject.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OO/createObject.html b/OO/createObject.html index 741bfda..780ba09 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -21,7 +21,7 @@ var person1 = Person('Nicholas', 29), person2 = Person('Greg', 27); - + //【工厂模式】 From 4255197285a96aef3d939ea7742f4598a8392ce8 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 16:55:04 +0800 Subject: [PATCH 30/50] update --- OO/createObject.html | 59 +++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 780ba09..a040faf 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -9,18 +9,21 @@ //【工厂模式】 -function Person(name, age) { - var o = new Object(); - o.name = name; - o.age = age; - o.sayName = function() { - console.log(this.name); - }; - return o; -} - -var person1 = Person('Nicholas', 29), - person2 = Person('Greg', 27); +// function Person(name, age) { +// var o = new Object(); +// o.name = name; +// o.age = age; +// o.sayName = function() { +// console.log(this.name); +// }; +// return o; +// } + +// var person1 = Person('Nicholas', 29), +// person2 = Person('Greg', 27); + +//工厂模式问题 +//工厂模式虽然解决了创建多个相似对象的问题,但却没解决对象识别的问题 //【工厂模式】 @@ -38,20 +41,36 @@ // var person1 = new Person('Nicholas', 29), // person2 = new Person('Greg', 27); +// console.log(person1.constructor === Person); //true +// console.log(person1 instanceof Object); //true +// console.log(person1 instanceof Person); //true + +//构造函数模式问题 +//对象的 constructor 属性最初是用来标识对象类型的。检测对象类型,还是 instanceof 更可靠。 +//创建的对象既是 Object 的实例,也是 Person 的实例。 +//创建自定义的构造函数意味着将来可以将它的实例标识为一种特定的类型;而这正是构造函数模式胜过工厂模式的地方。 + +//最大问题在于每个方法都会在实例上重新创建一遍 + //【构造函数模式】 //【原型模式】 -// function Person() {} -// Person.prototype.name = 'Nicholas'; -// Person.prototype.age = 29; -// Person.prototype.sayName = function() { -// console.log(this.name); -// }; +function Person() {} +Person.prototype.name = 'Nicholas'; +Person.prototype.age = 29; +Person.prototype.sayName = function() { + console.log(this.name); +}; + +var person1 = new Person(); +person1.sayName(); + +console.log(Person.prototype == person1.__proto__); //true -// var person1 = new Person(); -// person1.sayName(); +//原型模式问题 +//每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 //【原型模式】 From 357b4851f7310a91500cf5d836b69fb47dad6bb4 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 17:04:01 +0800 Subject: [PATCH 31/50] update --- OO/createObject.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/OO/createObject.html b/OO/createObject.html index a040faf..11e7049 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -67,10 +67,13 @@ var person1 = new Person(); person1.sayName(); -console.log(Person.prototype == person1.__proto__); //true +console.log(person1.__proto__ == Person.prototype); //true +console.log(Person.prototype.isPrototypeOf(person1)); //true +console.log(Object.getPrototypeOf(person1) == Person.prototype); //ES5方法 true //原型模式问题 //每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 +//指针[[Prototype]]存在于实例与构造函数的原型对象之间,在 Firefox、Safari 和 Chrome 在可以通过 __proto__ 访问。 //【原型模式】 From d1d4a00f150439ac63ee40f03b08145c99499514 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 23 Nov 2017 18:07:10 +0800 Subject: [PATCH 32/50] update --- OO/createObject.html | 50 +++++++++++++++++++++++++-------- OO/inherit.html | 66 ++++++++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 44 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 11e7049..1a10091 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -57,24 +57,52 @@ //【原型模式】 -function Person() {} -Person.prototype.name = 'Nicholas'; -Person.prototype.age = 29; -Person.prototype.sayName = function() { - console.log(this.name); -}; +// function Person() {} +// Person.prototype.name = 'Nicholas'; +// Person.prototype.age = 29; +// Person.prototype.sayName = function() { +// console.log(this.name); +// }; -var person1 = new Person(); -person1.sayName(); +// var person1 = new Person(); +// person1.sayName(); -console.log(person1.__proto__ == Person.prototype); //true -console.log(Person.prototype.isPrototypeOf(person1)); //true -console.log(Object.getPrototypeOf(person1) == Person.prototype); //ES5方法 true +// console.log(person1.__proto__ == Person.prototype); //true +// console.log(Person.prototype.isPrototypeOf(person1)); //true +// console.log(Object.getPrototypeOf(person1) == Person.prototype); //ES5方法 true //原型模式问题 //每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 //指针[[Prototype]]存在于实例与构造函数的原型对象之间,在 Firefox、Safari 和 Chrome 在可以通过 __proto__ 访问。 +// console.log(person1.hasOwnProperty('name')); + +//hasOwnProperty 可以检测属性是存在于实例上还是从原型继承来的 + +// console.log(Object.keys(Person.prototype)); +// person1.name = 'Greg'; +// console.log(Object.keys(person1)); + +//使用ES5的 Object.keys 方法也可以返回所有可枚举的属性。 + +// console.log(Object.getOwnPropertyNames(Person.prototype)); + +// getOwnPropertyNames 方法会返回所以属性,包括不可枚举的。上面两个方法都可以用来替代 for in循环。 + +function Person() {} + +Person.prototype = { + constructor: Person, + name: 'Nicholas', + age: 29, + sayName: function() { + console.log(this.name); + } +}; + +//可以通过对象字面量的方式书写原型对象,不过由于对原型的重写会丢掉 constructor 属性,可以手动指定。 +//不过这样会导致 constructor 属性的 [[Enumerable]] 为true + //【原型模式】 diff --git a/OO/inherit.html b/OO/inherit.html index 3a7d344..dc2c6ba 100644 --- a/OO/inherit.html +++ b/OO/inherit.html @@ -73,43 +73,43 @@ //【组合继承(伪经典继承)】 -function SuperType(name) { - this.name = name; - this.colors = ['red', 'blue', 'green']; -} - -SuperType.prototype.sayName = function() { - console.log(this.name); -}; - -function SubType(name, age) { - SuperType.call(this, name); //第二次调用 SuperType() - this.age = age; -} - -SubType.prototype = new SuperType(); //第一次调用 SuperType() -// console.log(SubType.prototype.constructor); //SuperType -SubType.prototype.constructor = SubType; -SubType.prototype.sayAge = function() { - console.log(this.age); -}; - -var instanceA = new SubType('AAA', 29); -instanceA.colors.push('black'); -console.log(instanceA.colors); -instanceA.sayName(); -instanceA.sayAge(); - -var instanceB = new SubType('BBB', 27); -console.log(instanceB.colors); -instanceB.sayName(); -instanceB.sayAge(); +// function SuperType(name) { +// this.name = name; +// this.colors = ['red', 'blue', 'green']; +// } + +// SuperType.prototype.sayName = function() { +// console.log(this.name); +// }; + +// function SubType(name, age) { +// SuperType.call(this, name); //第二次调用 SuperType() +// this.age = age; +// } + +// SubType.prototype = new SuperType(); //第一次调用 SuperType() +// // console.log(SubType.prototype.constructor); //SuperType +// SubType.prototype.constructor = SubType; +// SubType.prototype.sayAge = function() { +// console.log(this.age); +// }; + +// var instanceA = new SubType('AAA', 29); +// instanceA.colors.push('black'); +// console.log(instanceA.colors); +// instanceA.sayName(); +// instanceA.sayAge(); + +// var instanceB = new SubType('BBB', 27); +// console.log(instanceB.colors); +// instanceB.sayName(); +// instanceB.sayAge(); //组合继承问题 //组合继承融合了原型链与借用构造函数的优点,避免了二者的缺陷,是最常用的继承模式。 //由于调用了两次 SuperType,会分别在 SubType 的原型对象和实例对象上分别创建属性。 -console.log(instanceA); //instanceA.name & instanceA.__proto__.name +// console.log(SubType.prototype.hasOwnProperty('name')); //instanceA.name & instanceA.__proto__.name //【组合继承】 @@ -209,7 +209,7 @@ // var instanceA = new SubType('aaa', 26); -// console.log(instanceA); +// console.log(SubType.prototype.hasOwnProperty('name')); //寄生组合式继承问题 //上例的高效率体现在它只调用了一次 SuperType 构造函数,并且因此避免了在 SubType.prototype 上创建不必要的属性。寄生组合式继承是引用类型最理想的继承范式。 From 6542294ee9c16006909de0677a85f23b882dd2d1 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 13:55:55 +0800 Subject: [PATCH 33/50] update --- OO/createObject.html | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 1a10091..dbfdd07 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -89,19 +89,37 @@ // getOwnPropertyNames 方法会返回所以属性,包括不可枚举的。上面两个方法都可以用来替代 for in循环。 -function Person() {} +// function Person() {} -Person.prototype = { - constructor: Person, - name: 'Nicholas', - age: 29, - sayName: function() { - console.log(this.name); - } -}; +// Person.prototype = { +// constructor: Person, +// name: 'Nicholas', +// age: 29, +// sayName: function() { +// console.log(this.name); +// } +// }; //可以通过对象字面量的方式书写原型对象,不过由于对原型的重写会丢掉 constructor 属性,可以手动指定。 -//不过这样会导致 constructor 属性的 [[Enumerable]] 为true +//不过这样会导致 constructor 属性的 [[Enumerable]] 为true,可以通过ES5的 defineProperty 方法解决。 + +// function Person() {} + +// Person.prototype = { +// name: 'Nicholas', +// age: 29, +// sayName: function() { +// console.log(this.name); +// } +// }; + +// Object.defineProperty(Person.prototype, 'constructor', { +// enumerable: false, +// value: Person +// }); + +// console.log(Object.keys(Person.prototype)); +// console.log(Object.getOwnPropertyNames(Person.prototype)); //【原型模式】 From f036efc4cb9da286f0be75d8008bee7429b20fa7 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 15:01:05 +0800 Subject: [PATCH 34/50] update --- OO/createObject.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OO/createObject.html b/OO/createObject.html index dbfdd07..2063e71 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -72,7 +72,8 @@ // console.log(Object.getPrototypeOf(person1) == Person.prototype); //ES5方法 true //原型模式问题 -//每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 +//1. 每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 +//2. 不能传参 //指针[[Prototype]]存在于实例与构造函数的原型对象之间,在 Firefox、Safari 和 Chrome 在可以通过 __proto__ 访问。 // console.log(person1.hasOwnProperty('name')); From 6d7a692003da96e18ae64e518910311ee44b66c9 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 15:03:43 +0800 Subject: [PATCH 35/50] update --- OO/createObject.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OO/createObject.html b/OO/createObject.html index 2063e71..f18975c 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -148,6 +148,8 @@ // console.log(person2.friends); // console.log(person1.friends === person2.friends); //false // console.log(person1.sayName === person2.sayName); //true + +//这种构造函数和原型混合的模式,是目前使用最广、认同度最高的一种创建自定义类型的方法。 //【组合模式】 From b6051aa16c30f12e4af0ec2230cdf6ce216a86f4 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 15:16:09 +0800 Subject: [PATCH 36/50] update --- OO/createObject.html | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index f18975c..aa85427 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -155,16 +155,33 @@ //【动态原型模式】 -// function Person(name, age) { -// this.name = name; -// this.age = age; +function Person(name, age) { + this.name = name; + this.age = age; -// if (typeof this.sayName != 'function') { -// Person.prototype.sayName = function() { -// console.log(this.name); -// } -// } -// } + if (typeof this.sayName == 'function') return; + // console.log('create'); + Person.prototype.sayName = function() { + console.log(this.name); + } + + Person.prototype.sayAge = function() { + console.log(this.age); + } +} + +var person1 = new Person('xyx', 123), + person2 = new Person('yyy', 456); + +console.log(person1); +console.log(person2); + +person1.sayName(); +person2.sayAge(); + +//动态原型模式问题 +//动态原型模式实际是是对组合模式进行了一次包装,将对原型对象的初始化操作放入构造函数内部了。 +//对原型方法存在的验证可以避免对原型对象多次初始化。 //【动态原型模式】 From eceeeba2347563413602d69b2a54fc36802ff290 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 15:45:40 +0800 Subject: [PATCH 37/50] update --- OO/createObject.html | 89 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index aa85427..16172c8 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -155,29 +155,29 @@ //【动态原型模式】 -function Person(name, age) { - this.name = name; - this.age = age; - - if (typeof this.sayName == 'function') return; - // console.log('create'); - Person.prototype.sayName = function() { - console.log(this.name); - } - - Person.prototype.sayAge = function() { - console.log(this.age); - } -} +// function Person(name, age) { +// this.name = name; +// this.age = age; -var person1 = new Person('xyx', 123), - person2 = new Person('yyy', 456); +// if (typeof this.sayName == 'function') return; +// // console.log('create'); +// Person.prototype.sayName = function() { +// console.log(this.name); +// } + +// Person.prototype.sayAge = function() { +// console.log(this.age); +// } +// } -console.log(person1); -console.log(person2); +// var person1 = new Person('xyx', 123), +// person2 = new Person('yyy', 456); -person1.sayName(); -person2.sayAge(); +// console.log(person1); +// console.log(person2); + +// person1.sayName(); +// person2.sayAge(); //动态原型模式问题 //动态原型模式实际是是对组合模式进行了一次包装,将对原型对象的初始化操作放入构造函数内部了。 @@ -187,6 +187,22 @@ //【寄生构造函数模式】 +// function Person(name, age) { +// var o = new Object(); +// o.name = name; +// o.age = age; +// o.sayName = function() { +// console.log(this.name); +// }; +// return o; +// } + +// var person1 = new Person('Nicholas', 29), +// person2 = Person('xxx', 27); +// console.log(person1); +// console.log(person2); + +//例: // function SpecialArray() { // var values = new Array(); @@ -201,23 +217,38 @@ // var colors = new SpecialArray('red', 'blue', 'green'); // console.log(colors.toPipedString()); + +//寄生构造函数问题 +//寄生构造函数与工厂模式基本一致,唯一的区别的就是在调用时使用了 new 操作符。 +//与工厂模式一样,通过该模式生成的对象实例与构造函数之间并无关联,无法用 instanceof 来判断。 +//不推荐使用 + //【寄生构造函数模式】 //【稳妥构造函数模式】 -// function Person(name, age) { -// var o = new Object(); +function Person(name, age) { + var o = new Object(); -// o.sayName = function() { -// console.log(name); -// }; + //可以定义些私有变量 -// return o; -// } + o.sayName = function() { + console.log(name); + }; + + return o; +} + +var friend = Person('Nicholas', 29); +friend.sayName(); + +//稳妥构造函数问题 +//稳妥对象是指没有公共属性,而且其方法也不引用 this 的对象。 +//最适合用于禁止使用 this 和 new 的场景,以及防止外部改动。 +//与工厂模式不同的是,我们并没有将 name 和 age 这些属性挂载到对象上,只能通过 sayName 方法进行访问。 +//安全性 -// var friend = Person('Nicholas', 29); -// friend.sayName(); //【稳妥构造函数模式】 From df259af89bf4231b1cc01f1ec24f410233f6fb22 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Fri, 24 Nov 2017 16:02:16 +0800 Subject: [PATCH 38/50] update --- OO/inherit.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OO/inherit.html b/OO/inherit.html index dc2c6ba..aa2e668 100644 --- a/OO/inherit.html +++ b/OO/inherit.html @@ -22,10 +22,12 @@ // } // SubType.prototype = new SuperType(); - +// SubType.prototype.constructor = SubType; // SubType.prototype.getSubValue = function() { // return this.subproperty; // }; + +// // console.log(SubType.prototype.constructor); // var instance = new SubType(); // console.log(instance.getSuperValue()); From aa2dbcacfe735f0cd213c65106035f238d9acf5d Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Mon, 27 Nov 2017 17:28:39 +0800 Subject: [PATCH 39/50] update --- OO/createObject.html | 60 ++++++++++++++++++++++---------------------- OO/inherit.html | 2 ++ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 16172c8..4ce3fc0 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -155,29 +155,29 @@ //【动态原型模式】 -// function Person(name, age) { -// this.name = name; -// this.age = age; - -// if (typeof this.sayName == 'function') return; -// // console.log('create'); -// Person.prototype.sayName = function() { -// console.log(this.name); -// } - -// Person.prototype.sayAge = function() { -// console.log(this.age); -// } -// } +function Person(name, age) { + this.name = name; + this.age = age; + + if (typeof this.sayName == 'function') return; + // console.log('create'); + Person.prototype.sayName = function() { + console.log(this.name); + } + + Person.prototype.sayAge = function() { + console.log(this.age); + } +} -// var person1 = new Person('xyx', 123), -// person2 = new Person('yyy', 456); +var person1 = new Person('xyx', 123), + person2 = new Person('yyy', 456); -// console.log(person1); -// console.log(person2); +console.log(person1); +console.log(person2); -// person1.sayName(); -// person2.sayAge(); +person1.sayName(); +person2.sayAge(); //动态原型模式问题 //动态原型模式实际是是对组合模式进行了一次包装,将对原型对象的初始化操作放入构造函数内部了。 @@ -228,20 +228,20 @@ //【稳妥构造函数模式】 -function Person(name, age) { - var o = new Object(); +// function Person(name, age) { +// var o = new Object(); - //可以定义些私有变量 +// //可以定义些私有变量 - o.sayName = function() { - console.log(name); - }; +// o.sayName = function() { +// console.log(name); +// }; - return o; -} +// return o; +// } -var friend = Person('Nicholas', 29); -friend.sayName(); +// var friend = Person('Nicholas', 29); +// friend.sayName(); //稳妥构造函数问题 //稳妥对象是指没有公共属性,而且其方法也不引用 this 的对象。 diff --git a/OO/inherit.html b/OO/inherit.html index aa2e668..05388ac 100644 --- a/OO/inherit.html +++ b/OO/inherit.html @@ -182,6 +182,8 @@ //【寄生式继承】 + + //【寄生组合式继承】 // function inheritPrototype(subType, superType) { // var prototype = Object.create(superType.prototype); From 4c6b1be0a16ac3b29557f53f0a90801067a32372 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 29 Nov 2017 16:18:12 +0800 Subject: [PATCH 40/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/createObject.html | 39 ++++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.DS_Store b/.DS_Store index 16d134a81ecb58f497fe3ef68852ba4571c014cb..1b00726e8721c59a6baaaba883713780dcde6dcd 100644 GIT binary patch delta 42 ncmZn(XbITRB+g-MVXmWKWNAFPK*(|PY4Pp+C?b Date: Wed, 29 Nov 2017 16:33:32 +0800 Subject: [PATCH 41/50] update --- OO/createObject.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 74f3e0f..e0e7cf7 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -64,13 +64,13 @@ // console.log(this.name); // }; -// var person1 = new Person(); +var person1 = new Person(); // person1.sayName(); - -console.log(Person.prototype.constructor == Person); +// console.log(Person.prototype.constructor === Person); // console.log(person1.__proto__ == Person.prototype); //true // console.log(Person.prototype.isPrototypeOf(person1)); //true -// console.log(Object.getPrototypeOf(person1) == Person.prototype); //ES5方法 true +console.log(Object.getPrototypeOf(person1) === Person.prototype); //ES5方法 true +console.log(Object.getPrototypeOf(person1)); //原型模式问题 //1. 每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 From cc68a9c6c302b7db6daa4185a94541790187c463 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 29 Nov 2017 16:50:39 +0800 Subject: [PATCH 42/50] update --- OO/createObject.html | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index e0e7cf7..318a12d 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -58,19 +58,23 @@ //【原型模式】 function Person() {} -// Person.prototype.name = 'Nicholas'; +Person.prototype.name = 'Nicholas'; // Person.prototype.age = 29; // Person.prototype.sayName = function() { // console.log(this.name); // }; var person1 = new Person(); +person1.name = 'Daryl'; +console.log(person1.name); +delete person1.name; +console.log(person1.name); // person1.sayName(); // console.log(Person.prototype.constructor === Person); // console.log(person1.__proto__ == Person.prototype); //true // console.log(Person.prototype.isPrototypeOf(person1)); //true -console.log(Object.getPrototypeOf(person1) === Person.prototype); //ES5方法 true -console.log(Object.getPrototypeOf(person1)); +// console.log(Object.getPrototypeOf(person1) === Person.prototype); //ES5方法 true +// console.log(Object.getPrototypeOf(person1)); //原型模式问题 //1. 每个实例共享属性和方法,若一个实例更改引用类型属性,所有实例该属性均会被改变。 From d5e1dbc3a23958c6bc0245eb5c4e98721f5e5c2f Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Wed, 29 Nov 2017 17:49:13 +0800 Subject: [PATCH 43/50] update --- OO/createObject.html | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 318a12d..4a94836 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -65,10 +65,14 @@ // }; var person1 = new Person(); -person1.name = 'Daryl'; -console.log(person1.name); -delete person1.name; -console.log(person1.name); +person1.gender = 'male'; +person1.age = 29; +console.log(Object.keys(person1)); +console.log(Object.getOwnPropertyNames(Person.prototype)); +for (var o in person1) { + console.log(o); +} + // person1.sayName(); // console.log(Person.prototype.constructor === Person); // console.log(person1.__proto__ == Person.prototype); //true From 81196f9fcc4d2951dfaa2bed9f0525d2e890bfe4 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Thu, 30 Nov 2017 14:05:58 +0800 Subject: [PATCH 44/50] update --- OO/createObject.html | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/OO/createObject.html b/OO/createObject.html index 4a94836..4be71eb 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -57,21 +57,26 @@ //【原型模式】 -function Person() {} -Person.prototype.name = 'Nicholas'; -// Person.prototype.age = 29; -// Person.prototype.sayName = function() { -// console.log(this.name); -// }; +// function Person() {} +// Person.prototype.name = 'Nicholas'; +// // Person.prototype.age = 29; +// // Person.prototype.sayName = function() { +// // console.log(this.name); +// // }; + +// var person1 = new Person(); +// person1.gender = 'male'; +// person1.age = 29; +// console.log(Object.keys(person1)); +// console.log(Object.getOwnPropertyNames(Person.prototype)); +// for (var o in person1) { +// console.log(o); +// } -var person1 = new Person(); -person1.gender = 'male'; -person1.age = 29; -console.log(Object.keys(person1)); -console.log(Object.getOwnPropertyNames(Person.prototype)); -for (var o in person1) { - console.log(o); -} +String.prototype.reverseString = function() { + return this.split('').reverse().join(''); +}; +console.log('Hello World!'.reverseString()); // person1.sayName(); // console.log(Person.prototype.constructor === Person); From b7085496325feb3f6746ae51362aa6e82dfbac43 Mon Sep 17 00:00:00 2001 From: Darylxyx <921218060@qq.com> Date: Mon, 4 Dec 2017 11:16:31 +0800 Subject: [PATCH 45/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/inherit.html | 2 ++ 2 files changed, 2 insertions(+) diff --git a/.DS_Store b/.DS_Store index 1b00726e8721c59a6baaaba883713780dcde6dcd..b20f2d71cf5dea027d4074b5938df14d62cc5446 100644 GIT binary patch delta 42 ncmZn(XbITRB+g-KZmFYSWMMeDK*(|PY4Pp+C?b Date: Fri, 8 Dec 2017 14:57:16 +0800 Subject: [PATCH 46/50] update --- .DS_Store | Bin 10244 -> 10244 bytes OO/inherit.html | 43 ++++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.DS_Store b/.DS_Store index b20f2d71cf5dea027d4074b5938df14d62cc5446..a689b4b811d0d329b62998e2dfe12cc55639581a 100644 GIT binary patch delta 42 ncmZn(XbITRB+g-CWUQlLWMVkEK*(|PY4Pp+C?b Date: Fri, 8 Dec 2017 16:36:57 +0800 Subject: [PATCH 47/50] update --- OO/createObject.html | 1 + 1 file changed, 1 insertion(+) diff --git a/OO/createObject.html b/OO/createObject.html index 4be71eb..5b528ee 100644 --- a/OO/createObject.html +++ b/OO/createObject.html @@ -4,6 +4,7 @@ 创建对象 +