Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. var data = {'储存': ['32GB', '64GB'],
  12. '内存': ['2GB', '4GB'],
  13. '屏幕': ['5英寸', '4英寸']}
  14.  
  15. console.log('处理结果:', handleAll(data))
  16.  
  17. // --- Object.create() 方法 (3)
  18.  
  19. appData = Object.create(Object.prototype, {
  20. _data: {
  21. configurable: false,
  22. enumerable: false,
  23. writable: true,
  24. value: {}
  25. },
  26. data: {
  27. configurable: false,
  28. enumerable: true,
  29. set: function (v) {
  30. this._data = v
  31. },
  32. get: function () {
  33. return handleAll(this._data)
  34. }
  35. }
  36. })
  37.  
  38. appData.data = data
  39. console.log(appData.data)
  40.  
  41. // --- 构建函数方法 (2)
  42.  
  43. function Phones (data) {
  44. this.data = data
  45. }
  46.  
  47. Phones.prototype = {
  48. format: function () {
  49. return handleAll(this.data)
  50. }
  51. }
  52.  
  53. var phones = new Phones(data)
  54. console.log(phones.format())
  55.  
  56. // --- 原型链方法 (1)
  57.  
  58. Object.prototype.format = function () {
  59. return handleAll(this)
  60. }
  61. console.log(data.format())
  62.  
  63. // --- 基础方法 (0)
  64.  
  65. function handleAll (data) {
  66. var res = []
  67. Object.keys(data).forEach(v => {
  68. res = pushData (completeData(data[v], v), res)
  69. })
  70. return res
  71. }
  72.  
  73. function pushData (arr, result) {
  74. if (result.length === 0) {
  75. return arr
  76. }
  77.  
  78. var newResult = []
  79. arr.forEach(v => {
  80. newResult = newResult.concat(plusData(result, v))
  81. })
  82.  
  83. return newResult
  84. }
  85.  
  86. function completeData (arr, string) {
  87. return arr.map(v => {
  88. return string + ': ' + v
  89. })
  90. }
  91.  
  92. function plusData (arr, string) {
  93. return arr.map(v => {
  94. return v + ', ' + string
  95. })
  96. }
  97. </script>
  98.  
  99.  
  100.  
  101. <script id="jsbin-source-javascript" type="text/javascript">var data = {'储存': ['32GB', '64GB'],
  102. '内存': ['2GB', '4GB'],
  103. '屏幕': ['5英寸', '4英寸']}
  104.  
  105. console.log('处理结果:', handleAll(data))
  106.  
  107. // --- Object.create() 方法 (3)
  108.  
  109. appData = Object.create(Object.prototype, {
  110. _data: {
  111. configurable: false,
  112. enumerable: false,
  113. writable: true,
  114. value: {}
  115. },
  116. data: {
  117. configurable: false,
  118. enumerable: true,
  119. set: function (v) {
  120. this._data = v
  121. },
  122. get: function () {
  123. return handleAll(this._data)
  124. }
  125. }
  126. })
  127.  
  128. appData.data = data
  129. console.log(appData.data)
  130.  
  131. // --- 构建函数方法 (2)
  132.  
  133. function Phones (data) {
  134. this.data = data
  135. }
  136.  
  137. Phones.prototype = {
  138. format: function () {
  139. return handleAll(this.data)
  140. }
  141. }
  142.  
  143. var phones = new Phones(data)
  144. console.log(phones.format())
  145.  
  146. // --- 原型链方法 (1)
  147.  
  148. Object.prototype.format = function () {
  149. return handleAll(this)
  150. }
  151. console.log(data.format())
  152.  
  153. // --- 基础方法 (0)
  154.  
  155. function handleAll (data) {
  156. var res = []
  157. Object.keys(data).forEach(v => {
  158. res = pushData (completeData(data[v], v), res)
  159. })
  160. return res
  161. }
  162.  
  163. function pushData (arr, result) {
  164. if (result.length === 0) {
  165. return arr
  166. }
  167.  
  168. var newResult = []
  169. arr.forEach(v => {
  170. newResult = newResult.concat(plusData(result, v))
  171. })
  172.  
  173. return newResult
  174. }
  175.  
  176. function completeData (arr, string) {
  177. return arr.map(v => {
  178. return string + ': ' + v
  179. })
  180. }
  181.  
  182. function plusData (arr, string) {
  183. return arr.map(v => {
  184. return v + ', ' + string
  185. })
  186. }
  187.  
  188. </script></body>
  189. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement