123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- //HTML tree
- /*
- {
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
-
- function htmlTree(obj){
- let string = ""
- let tag = obj.tagName
- if(tag){
- string = `<${tag}`
- let attrb = obj.attrs
- if(attrb){
- for(nameAttrb in attrb){
- string += ` style ${nameAttrb} = ${obj.attrs[nameAttrb]}`
- }
- }
- string += '>'
- for(child of obj.children){
- string += htmlTree(child)
- }
- string+=`</${tag}>`
-
- }
- else {
- string = obj
- }
- return string
- }
- document.write(htmlTree(table)) //вернет <table border='1' ..
- console.log(htmlTree(table))
- }
- //DOM tree
- {
-
-
- function domTree (parent,obj) {
- if(obj.tagName){
- let tag = document.createElement(obj.tagName)
- if(obj.attrs){
- for(nameAttrb in obj.attrs){
- tag[nameAttrb] = obj.attrs[nameAttrb]
- }
- }
- parent.append(tag)
- for(child of obj.children){
-
- domTree(tag, child)
- }
- }
- else {
- parent.append(obj)
- }
-
- }
-
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- domTree(document.body, table)
- }
- //Deep Copy
- {
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
-
- function deepCopy (obj) {
- let copy
- if (Array.isArray(obj)) {
- copy =[]
- }
- else {
- copy = {}
- }
- if(obj){
- Object.keys(obj).forEach(x=>{
- if(typeof obj[x] === Object){
- copy[x] = deepCopy(obj[x])
- }
- else copy[x] = obj[x]
- })
- }
- return copy
- }
- const arr = [1,"string", null, undefined, {a: 15, b: 10, c: [1,2,3,4],d: undefined, e: true }, true, false]
- const arr2 = deepCopy(arr) //arr2 и все его вложенные массивы и объекты - другие объекты, которые можно менять без риска поменять что-то в arr
- const table2 = deepCopy(table) //аналогично
- console.log(arr2)
- console.log(table2)
- }
- */
- //My Stringify
- {
- function stringify (obj) {
- let result = ``
- if(typeof(obj) === "number" || typeof(obj) === "boolean" || obj === null){
- return result += obj
- }
-
- if(typeof(obj) === "string"){
- return result += `"${obj}"`
- }
- if(Array.isArray(obj)){
- result += "["
- if(obj.length > 0){
- for(value of obj){
- if(value === undefined){
- result += `${stringify(null)}`
- }
- result += `${stringify(value)},`
- }
- result = result.slice(0, result.length - 1)
- }
- result += "]"
- return result
-
- }
- if(typeof(obj) === "object"){
- result += '{'
-
-
- if(Object.keys(obj).length > 0){
- for(key in obj){
- if(obj[key] === undefined){
- continue
- }
- result += `"${key}":`
- result += `${stringify(obj[key])},`
- }
- result = result.slice(0, result.length - 1)
- }
- result += '}'
- }
-
- return result
- }
- const arr = [1,"string", null, undefined, {a: 15, b: 10, c: [1,2,3,4],d: undefined, e: true }, true, false]
- const jsonString = stringify(arr) //напишите функцию stringify без использования JSON.stringify
- console.log(jsonString)
- console.log(JSON.parse(jsonString)) //не должно поломаться и должно вернуть структуру, во всем схожую с оригинальным arr или table
-
- }
|