Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- var data = {'储存': ['32GB', '64GB'],
- '内存': ['2GB', '4GB'],
- '屏幕': ['5英寸', '4英寸']}
- console.log('处理结果:', handleAll(data))
- // --- Object.create() 方法 (3)
- appData = Object.create(Object.prototype, {
- _data: {
- configurable: false,
- enumerable: false,
- writable: true,
- value: {}
- },
- data: {
- configurable: false,
- enumerable: true,
- set: function (v) {
- this._data = v
- },
- get: function () {
- return handleAll(this._data)
- }
- }
- })
- appData.data = data
- console.log(appData.data)
- // --- 构建函数方法 (2)
- function Phones (data) {
- this.data = data
- }
- Phones.prototype = {
- format: function () {
- return handleAll(this.data)
- }
- }
- var phones = new Phones(data)
- console.log(phones.format())
- // --- 原型链方法 (1)
- Object.prototype.format = function () {
- return handleAll(this)
- }
- console.log(data.format())
- // --- 基础方法 (0)
- function handleAll (data) {
- var res = []
- Object.keys(data).forEach(v => {
- res = pushData (completeData(data[v], v), res)
- })
- return res
- }
- function pushData (arr, result) {
- if (result.length === 0) {
- return arr
- }
- var newResult = []
- arr.forEach(v => {
- newResult = newResult.concat(plusData(result, v))
- })
- return newResult
- }
- function completeData (arr, string) {
- return arr.map(v => {
- return string + ': ' + v
- })
- }
- function plusData (arr, string) {
- return arr.map(v => {
- return v + ', ' + string
- })
- }
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">var data = {'储存': ['32GB', '64GB'],
- '内存': ['2GB', '4GB'],
- '屏幕': ['5英寸', '4英寸']}
- console.log('处理结果:', handleAll(data))
- // --- Object.create() 方法 (3)
- appData = Object.create(Object.prototype, {
- _data: {
- configurable: false,
- enumerable: false,
- writable: true,
- value: {}
- },
- data: {
- configurable: false,
- enumerable: true,
- set: function (v) {
- this._data = v
- },
- get: function () {
- return handleAll(this._data)
- }
- }
- })
- appData.data = data
- console.log(appData.data)
- // --- 构建函数方法 (2)
- function Phones (data) {
- this.data = data
- }
- Phones.prototype = {
- format: function () {
- return handleAll(this.data)
- }
- }
- var phones = new Phones(data)
- console.log(phones.format())
- // --- 原型链方法 (1)
- Object.prototype.format = function () {
- return handleAll(this)
- }
- console.log(data.format())
- // --- 基础方法 (0)
- function handleAll (data) {
- var res = []
- Object.keys(data).forEach(v => {
- res = pushData (completeData(data[v], v), res)
- })
- return res
- }
- function pushData (arr, result) {
- if (result.length === 0) {
- return arr
- }
- var newResult = []
- arr.forEach(v => {
- newResult = newResult.concat(plusData(result, v))
- })
- return newResult
- }
- function completeData (arr, string) {
- return arr.map(v => {
- return string + ': ' + v
- })
- }
- function plusData (arr, string) {
- return arr.map(v => {
- return v + ', ' + string
- })
- }
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement