123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //Literals
- {
- var ford = {
- type: "hybrid",
- model: "fusion",
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- }
- //Literals expand
- var ford = {
- type: "hybrid",
- [prompt('Введите название ключа')]: [prompt('Введите параметр')].join(' '),
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- //Literals copy
- let property = prompt('Введите свойство')
- const obj = {...ford}
- obj.type = property
- console.log(obj)
- //Html tree
- let body = {
- tagName: 'body',
- children: [{
- tagName: 'div',
- children: [{
- tagName: 'span',
- children: ["Enter a data please:"]
- },
- {
- tagName: 'br',
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'name',
- }
- },
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'surname',
- }
- },
- }]
- },
- {
- tagName: 'div',
- children: [{
- tagName: 'button',
- attrs: {
- [attributes]: "ok",
- },
- children:['OK']
- },
- {
- tagName: 'button',
- attrs: {},
- }]
- }]
-
- }
- body.children[1].children[1].children=["Cancel"];
- body.children[1].children[1].attrs.id= "cancel";
- //Parent
- body.children[0].parent = body;
- body.children[1].parent = body;
- body.children[0].children[0].parent = body.children[0];
- body.children[0].children[1].parent = body.children[0];
- body.children[0].children[2].parent = body.children[0];
- body.children[0].children[3].parent = body.children[0];
- body.children[1].children[0].parent = body.children[1];
- body.children[1].children[1].parent = body.children[1];
- //destruct array
- {
- let arr = [1,2,3,4,5, "a", "b", "c"]
- let [odd1,even1,odd2,even2,odd3,...arr2]=arr
- console.log(odd1,even1,odd2,even2,odd3,arr2)//1 2 3 4 5 [ 'a', 'b', 'c' ]
- }
- //destruct string
- {
- let arr = [1, "abc"]
- let [number,arr2]=arr
- let [s1,s2,s3]=arr2
- console.log(number,s1,s2,s3)// 1 a b c
- }
- //destruct 2
- {
- let obj = {
- name: 'Ivan',
- surname: 'Petrov',
- children: [{name: 'Maria'}, {name: 'Nikolay'}]
- }
- let {children:[{name:name1},{name:name2}]}= obj
- console.log(name1,name2)//Maria Nikolay
- }
- //destruct 3
- {
- let arr = [1,2,3,4, 5,6,7,10]
- let {0:a,1:b,length}= arr
- console.log(a,b,length)//1 2 8
- }
- //Destructure и Change OK
- {
- let attributes = prompt('Введите название атрибута')// это к заданию Change OK
- let body = {
- tagName: 'body',
- children: [{
- tagName: 'div',
- children: [{
- tagName: 'span',
- children: ["Enter a data please:"]
- },
- {
- tagName: 'br',
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'name',
- }
- },
- },
- {
- tagName: 'input',
- attrs: {
- type:'text',
- id:'surname',
- },
- }]
- },
- {
- tagName: 'div',
- children: [{
- tagName: 'button',
- attrs: {
- [attributes]: "ok",
- },
- children:['OK']
- },
- {
- tagName: 'button',
- children:["Cancel"],
- attrs: {
- id: "cancel"
- },
- }]
- }]
-
- }
- let {children:[{children:[{children:[textSpan]},{},{},{attrs:{id:textSurname}}]},{children:[{},{children:[textCancel]}]}]}=body//это к заданию Destructure
- console.log(textSpan,textCancel,textSurname)//Enter a data please: Cancel surname //это к заданию Destructure
- body.children[1].children[0].attrs.attributes= prompt('Введите значение атрибута');// это к заданию Change OK
- }
- //Copy delete
- {
- let ford = {
- type: "hybrid",
- model: "fusion",
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- const obj= {...ford}
- delete obj[prompt('Введите ключ для удаления')]
- }
|