Skip to content

Instantly share code, notes, and snippets.

@iugo
Forked from anonymous/index.html
Created January 23, 2017 02:36
Show Gist options
  • Select an option

  • Save iugo/d2e4a0997fdd5954fc770cbd0268c77e to your computer and use it in GitHub Desktop.

Select an option

Save iugo/d2e4a0997fdd5954fc770cbd0268c77e to your computer and use it in GitHub Desktop.
JavaScript 笔试题 JS Bin// source https://jsbin.com/mowubenaji
<!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>
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
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment