anton123 2 years ago
parent
commit
e39cdf874f
2 changed files with 437 additions and 0 deletions
  1. 155 0
      foop.js
  2. 282 0
      Рекурсия.js

+ 155 - 0
foop.js

@@ -0,0 +1,155 @@
+//Person Constructor
+{
+    class Person {
+        constructor(name,surname){
+            this.name = name
+            this.surname = surname
+        }
+        getFullName(){
+            if(this.fatherName){
+                return `${this.name} ${this.fatherName} ${this.surname}`
+            }
+            else{return `${this.name} ${this.surname}`}
+        }
+    }
+            
+    const a = new Person("Вася", "Пупкин")
+    const b = new Person("Анна", "Иванова")
+    const c = new Person("Елизавета", "Петрова")
+    
+    console.log(a.getFullName()) //Вася Пупкин
+    a.fatherName = 'Иванович' 
+    console.log(a.getFullName()) //Вася Иванович Пупкин
+    
+    console.log(b.getFullName()) //Анна Иванова
+}
+
+
+//Person Prototype
+{
+    function createPerson(name,surname){
+        class Person {
+            constructor(name,surname){
+                this.name = name
+                this.surname = surname
+            }
+            getFullName(){
+                if(this.fatherName){
+                    return `${this.name} ${this.fatherName} ${this.surname}`
+                }
+                else{return `${this.name} ${this.surname}`}
+            }
+        }
+        return new Person(name, surname)
+    }
+    
+    const a = createPerson("Вася", "Пупкин")
+    const b = createPerson("Анна", "Иванова")
+    const c = createPerson("Елизавета", "Петрова")
+
+    console.log(a.getFullName()) //Вася Пупкин
+    a.fatherName = 'Иванович'    
+    console.log(a.getFullName()) //Вася Иванович Пупкин
+
+    console.log(b.getFullName()) //Анна Иванова
+}
+
+
+//Store
+{
+    function createStore(reducer){
+        let state       = reducer(undefined, {}) 
+        let cbs         = []                     
+        
+        function Store(){ 
+            
+            this.getState  = () => state    
+            this.subscribe = cb => (cbs.push(cb), () => cbs = cbs.filter(c => c !== cb)) 
+            this.dispatch  = action => { 
+                const newState = reducer(state, action) 
+                if (newState !== state){ 
+                    state = newState 
+                    for (let cb of cbs)  cb() 
+                }
+            }
+        }
+        
+        return new Store()
+    }
+}
+
+
+//Password
+{
+    function Password(parent, open) {
+        let input = document.createElement('input')
+        let checkBox = document.createElement('input')
+        checkBox.type = 'checkbox'
+        parent.append(input, checkBox)
+
+        this.setValue =(value)=> input.value = value
+        this.setOpen =(open)=> input.type = open ?'text':'password'
+
+        this.getValue =()=> input.value
+        this.getOpen =()=> input.type
+
+        checkBox.onchange =()=>this.setOpen(checkBox.checked)
+    }
+
+    let p = new Password(document.body, true)
+
+    p.onChange = data => console.log(data)
+    p.onOpenChange = open => console.log(open)
+    
+    p.setValue('qwerty')
+    console.log(p.getValue())
+    
+    p.setOpen(false)
+    console.log(p.getOpen()) 
+}
+
+
+//LoginForm
+{
+    function Password(parent, open) {
+        let inputPass = document.createElement('input')
+        let inputLogin = document.createElement('input')
+        let checkBox = document.createElement('input')
+        let button = document.createElement('button')
+        button.innerText='Войти'
+        button.disabled=true
+        checkBox.type = 'checkbox'
+        parent.append(inputPass,inputLogin,checkBox,button)
+
+        this.setValue =(value)=> inputPass.value = value
+        this.setOpen =(open)=> inputPass.type= open ?'text':'password'
+
+        this.getValue =()=> inputPass.value
+        this.getOpen =()=> inputPass.type
+
+        checkBox.onchange =()=>this.setOpen(checkBox.checked)
+        
+        function btn (){
+            if(inputPass.value && inputLogin.value){
+                button.disabled=false
+            }
+            else{
+                button.disabled=true
+            }
+        }
+
+        inputPass.oninput = btn
+        inputLogin.oninput = btn
+    }
+
+    let p = new Password(document.body, true)
+
+    p.onChange = data => console.log(data)
+    p.onOpenChange = open => console.log(open)
+    
+    p.setValue('qwerty')
+    console.log(p.getValue())
+    
+    p.setOpen(false)
+    console.log(p.getOpen()) 
+}

+ 282 - 0
Рекурсия.js

@@ -0,0 +1,282 @@
+// Рекурсия: HTML tree
+{
+    function htmlTree(parent){
+        let strHtml
+        if (parent.tagName) {
+            strHtml = `<${parent.tagName}`
+            
+            if (parent.attrs) {
+                for (const attr in parent.attrs) {
+                    strHtml += ` ${attr} = ${parent.attrs[attr]}`
+                }
+            }
+            strHtml += '>'
+
+            for (const child of parent.children) {
+                strHtml += htmlTree(child)
+            }
+            strHtml += `</${parent.tagName}>`
+        }
+
+        else {
+            strHtml = parent
+        }
+
+        return strHtml
+    }
+
+
+    let container = document.createElement('container')
+    container.innerHTML= htmlTree(table)
+    document.body.append(container)
+}
+
+//Рекурсия: DOM tree
+{
+    function domTree(parentHtmlTree,jsTree){
+        
+        if (jsTree.tagName) {
+            let tag = document.createElement(jsTree.tagName)
+            
+            if (jsTree.attrs) {
+                for (const attr in jsTree.attrs) {
+                    tag[attr] = jsTree.attrs[attr]
+                }
+            }
+            parentHtmlTree.append(tag)
+           
+
+            for (const child of jsTree.children) {
+                domTree(tag,child)
+            }
+            
+        }
+
+        else {
+            parentHtmlTree.append(jsTree)
+        }
+        
+
+        
+    }
+
+
+    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)
+}
+
+
+//Рекурсия : Глубокое копирование
+{
+    function deepCopy(parent){
+        let data 
+        
+        if(Array.isArray(parent)){
+            data=[]
+        }
+        else if(typeof parent === "object" && !Array.isArray(parent) && parent != null){
+            data={}
+        }
+        else{
+            return data=parent
+        }
+        
+        for(let el in parent){
+            data[el] = deepCopy(parent[el])
+        }
+
+        return data
+    }
+    
+    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"],
+                    },
+                ]
+            }
+        ]
+    }
+    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) //аналогично
+}
+
+
+//Рекурсия : My Stringify
+
+{
+    //let text=''
+    function stringify(parent){
+        let text=''
+        let data 
+        let type
+        if(Array.isArray(parent)){
+            data=[]
+            text+='['
+        }
+        else if(typeof parent === "object" && !Array.isArray(parent) && parent != null){
+            data={}
+            text+='{'
+            type="object"
+        }
+        else{
+           
+            return data=parent
+        }
+        let i = 0
+        for(let el in parent){
+            if(type==="object"){
+                text+= '"'+el+'":'
+            }
+            data[el] = stringify(parent[el])
+            i++
+            
+            if(typeof data[el] === 'number'|| data[el]===null || typeof data[el] === 'boolean'){
+                text+=data[el]+','
+            }
+            else if(typeof data[el] === 'string'){
+                text+='"'+data[el]+'",'
+            }
+            else if(typeof data[el] === 'undefined'){
+                text+= null+','
+            }
+            
+            if(parent.length===i || Object.keys(parent).length===i){
+                text=text.slice(0,-1)
+                type==="object"? text+= '},':text+= '],'
+            }
+
+            
+        }
+        
+        return text.split('}"').join('}').split(']"').join(']').split('"{').join('{').split('"[').join('[').slice(0,-1)
+    }
+
+    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(JSON.parse(jsonString)) //не должно поломаться и должно вернуть структуру, во всем схожую с оригинальным arr или table
+}
+
+
+
+//Рекурсия : My Stringify
+{
+    function stringify(parent){
+        let text=''
+        function stringifyNested(parent){
+            let data 
+            let type
+            if(Array.isArray(parent)){
+                data=[]
+                text+='['
+            }
+            else if(typeof parent === "object" && !Array.isArray(parent) && parent != null){
+                data={}
+                text+='{'
+                type="object"
+            }
+            else{
+            
+                return data=parent
+            }
+            let i = 0
+            for(let el in parent){
+                if(type==="object"){
+                    text+= '"'+el+'":'
+                }
+                data[el] = stringifyNested(parent[el])
+                i++
+                
+                if(typeof data[el] === 'number'|| data[el]===null || typeof data[el] === 'boolean'){
+                    text+=data[el]+','
+                }
+                else if(typeof data[el] === 'string'){
+                    text+='"'+data[el]+'",'
+                }
+                else if(typeof data[el] === 'undefined'){
+                    text+= null+','
+                }
+                
+                if(parent.length===i || Object.keys(parent).length===i){
+                    text=text.slice(0,-1)
+                    type==="object"? text+= '},':text+= '],'
+                }
+
+                
+            }
+            
+            return data
+        }
+        stringifyNested(parent)
+        return text.slice(0,-1)
+    }
+    
+
+    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(JSON.parse(jsonString)) //не должно поломаться и должно вернуть структуру, во всем схожую с оригинальным arr или table
+}
+
+
+  
+
+