Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="[add your bin description]">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /**
  13. 인수로서의 객체와 함수 기본 2
  14. 이벤트를 이용한 Todo 앱 개선
  15.  
  16.  
  17. var todos = [];
  18.  
  19. function newTodo(event) {
  20. let $ul = $('#todo-list');
  21. let $title = $('#title');
  22. let todosHTML;
  23.  
  24. // val:입력타입관계없이 호출
  25. todos.push($title.val());
  26.  
  27. // 입력후 비워준다.
  28. $title.val('');
  29. todosHTML = '';
  30.  
  31. for(let i=0; i < todos.length; i++) {
  32. todosHTML = `${todosHTML}<li>${todos[i]}</li>`;
  33. }
  34.  
  35. $ul.html(todosHTML);
  36. }
  37.  
  38. function app() {
  39. var $newTodo = $('#new-todo');
  40. //on:addeventListener
  41. $newTodo.on('click', newTodo);
  42. }
  43.  
  44. // onload시 제이쿼리가 괄호안에 함수를 호출한다.
  45. $(app);
  46. **/
  47.  
  48. /**
  49. 인수로서의 객체와 함수 기본 2
  50. 함수 인수 다루기 - 실행의 위임
  51. 언제일어날지 알수없을때,,,
  52.  
  53. function add(a, b, fn) {
  54. let result;
  55.  
  56. result = a + b;
  57.  
  58. fn(result);
  59. }
  60.  
  61. function finishAdd(sum) {
  62. console.log(sum);
  63. }
  64.  
  65. //콜백함수
  66. // 콜백함수의 예 setTimeout(함수명, 2000) 2초뒤 함수호출
  67. add(100, 200, finishAdd);
  68.  
  69. //위와동일
  70. add(100, 200, function(sum) {
  71. console.log(sum);
  72. });
  73.  
  74. add(100, 200, sum => console.log(sum));
  75. **/
  76.  
  77.  
  78.  
  79. /**
  80. 인수로서의 객체와 함수 기본 1
  81. 객체 인수
  82.  
  83. var todo = {
  84. items: [],
  85. status: {
  86. allCount: 0,
  87. completeCount: 0
  88. },
  89.  
  90. //겟터: 뺄때
  91. get allCount() {
  92. return this.status.allCount;
  93. },
  94.  
  95. // 셋터: 안에서는 함수지만 밖에선 속성,
  96. set allCount(count) {
  97. console.warn('임의로 할 일 개수를 설정할 수 없습니다, addItem 기능을 사용해 주십시오.');
  98. },
  99.  
  100. // 2번째 값이 없으면 옵션 { }안의 값을 default로 갖는다.
  101. addItem: function(title, options = {}) {
  102. let newTodo = {
  103. title: title
  104. };
  105.  
  106.  
  107.  
  108. newTodo.date = !!options.date ? options.date : '2017-06-17';
  109. newTodo.complete = !!options.complete;
  110.  
  111. this.items.push(newTodo);
  112.  
  113. this.status.allCount++;
  114.  
  115. if (newTodo.complete) {
  116. this.incrementCompleteTodo();
  117. }
  118. },
  119.  
  120. completeTodo(index) {
  121. this.items[index].complete = true;
  122. this.incrementCompleteTodo();
  123. },
  124.  
  125. incrementCompleteTodo() {
  126. this.status.completeCount++;
  127. }
  128. };
  129.  
  130. todo.addItem('객체와 함수 학습', {
  131. date: '2017-06-17'
  132. });
  133.  
  134. todo.addItem('객체 내의 객체 학습');
  135. todo.addItem('객체 인수 다루기', {
  136. complete: true
  137. });
  138.  
  139. **/
  140.  
  141. /**
  142. 인수로서의 객체와 함수 기본 1
  143. 객체 인수
  144.  
  145. var todo = {
  146. items: [],
  147. status: {
  148. allCount: 0,
  149. completeCount: 0
  150. },
  151.  
  152. addItem: function(title, options) {
  153. let newTodo = {
  154. title: title
  155. };
  156.  
  157. if (options) {
  158. if (options.date) {
  159. newTodo.date = options.date;
  160. } else {
  161. newTodo.date = '2017-06-17';
  162. }
  163.  
  164. newTodo.complete = options.complete ? options.complete : false;
  165. // newTodo.complete = !!options.complete;
  166. // newTodo.complete = option.complete || false;
  167. }
  168.  
  169. this.items.push(newTodo);
  170.  
  171. this.status.allCount++;
  172.  
  173. if (newTodo.complete) {
  174. this.status.completeCount++;
  175. }
  176. },
  177.  
  178. completeTodo(index) {
  179. this.items[index].complete = true;
  180. this.status.completeCount++;
  181. }
  182. };
  183.  
  184. todo.addItem('객체와 함수 학습', {
  185. date: '2017-06-17'
  186. });
  187.  
  188. todo.addItem('객체 내의 객체 학습');
  189. todo.addItem('객체 인수 다루기', {
  190. complete: true
  191. });
  192.  
  193. console.log(todo.status);
  194. **/
  195.  
  196.  
  197.  
  198. /**
  199. 인수로서의 객체와 함수 기본 1
  200. 가변 인수 다루기
  201. **/
  202. function add1(a, b) {
  203. return a + b;
  204. }
  205.  
  206. //유사배열
  207. function add2() {
  208. return arguments[0] + arguments[1];
  209. }
  210.  
  211. //최근추가된 스팩, ...가변인수
  212. function add3(...args) {
  213. return args[0] + args[1];
  214. }
  215.  
  216. // 명확하지않은 인수의 오동작을 방지한다
  217. function add21() {
  218. let result = 0;
  219.  
  220. for(let i=0; i < arguments.length; i++) {
  221. result += arguments[i];
  222. }
  223.  
  224. return result;
  225. }
  226.  
  227. //위와같음.
  228. function add31(...args) {
  229. let result = 0;
  230.  
  231. for(let i=0; i < args.length; i++) {
  232. result += args[i];
  233. }
  234.  
  235. return result;
  236. }
  237.  
  238. //arguments와 동시사용가능
  239. function add32(a, ...args) {
  240. let result = a;
  241.  
  242. for(let i=0; i < args.length; i++) {
  243. result += args[i];
  244. }
  245.  
  246. return result;
  247. }
  248.  
  249.  
  250.  
  251.  
  252. /**
  253. 객체의 속성과 메소드
  254.  
  255. var todo = {
  256. items: [],
  257. status: {
  258. allCount: 0,
  259. completeCount: 0
  260. },
  261.  
  262. addItem: function(title, date) {
  263. this.items.push({
  264. title: title,
  265. date: date,
  266. complete: false
  267. });
  268.  
  269. this.status.allCount++;
  270. },
  271.  
  272. //축약형. 함수. 객체안에서만 쓸수있다
  273. completeTodo(index) {
  274. this.items[index].complete = true;
  275. this.status.completeCount++;
  276. }
  277. };
  278.  
  279. todo.addItem('객체와 함수 학습', '2017-06-17');
  280. todo.addItem('객체 내의 객체 학습', '2017-06-17');
  281.  
  282. todo.completeTodo(1);
  283.  
  284. console.log(todo.status);
  285.  
  286. **/
  287.  
  288.  
  289.  
  290. /**
  291. 함수 내의 변수
  292. var 대신 let을써도됨(scope와 연관)
  293. 전역객체: window라는 객체아래생성, window.은 생략가능.
  294.  
  295. Let은 가장 가까운 블럭을 찾고, var는 가장가까운 함수를 찾는다..
  296.  
  297. function add100(a) {
  298. let x = 100;
  299.  
  300. return a+x;
  301. }
  302.  
  303. var result = add100(400);
  304.  
  305. console.log(result);
  306. console.log(x);
  307. **/
  308.  
  309.  
  310.  
  311.  
  312. /**
  313. 함수의 기본 형태 4가지
  314.  
  315. var result;
  316.  
  317. function add1(a, b) {
  318. return a + b;
  319. }
  320.  
  321. var add2 = function(a, b) {
  322. return a + b;
  323. }
  324.  
  325. // => function과 같은기능. 한줄함수(한줄이상도가능하나 한줄이상이면 return써야함)
  326. // => 다음에 나오는것을 값을인식, 자동return
  327. // 최근브라우저는 모두지원 ie10이하는 지원안함. 난해함이없어 다루기쉽다.
  328. var add3 = (a, b) => a + b;
  329.  
  330.  
  331.  
  332.  
  333. result = add1(10, 20);
  334. result = add2(10, 20);
  335. result = add3(10, 20);
  336.  
  337. //이름없는함수를 즉시실행할때..
  338. result = (function(a, b) {
  339. return a + b;
  340. })(10, 20);
  341.  
  342. //위와동일. arrow함수로 변형
  343. result = ((a, b) => a + b)(10, 20);
  344.  
  345. **/
  346.  
  347.  
  348. /*
  349. push:추가한다.
  350.  
  351.  
  352. var todo = {
  353. items: [],
  354. status: {
  355. allCount: 0,
  356. completeCount:0
  357. }
  358. };
  359.  
  360.  
  361. // 원래동일한말,, 해석하기쉽게 todo.items.length == todo.status.allCount;
  362.  
  363.  
  364. todo.items.push ({
  365. title: 'aaa',
  366. date: '',
  367. complete: false
  368. });
  369.  
  370. todo.status.allCount++;
  371.  
  372. */
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382. /* .을이용해 단계별접근. 건너띄고 바로접근안됨
  383. var somthingObject = {
  384. name: 'something',
  385. x: {
  386. x1: {
  387.  
  388. }
  389.  
  390. }
  391. };
  392.  
  393. somethingObject.x.x1
  394.  
  395. */
  396.  
  397.  
  398.  
  399.  
  400. /**
  401. + 객체 속성 추가하기 - 동적 생성
  402. 객체뒤 .을 이용해 객체추가생성, 위아래동일하다,,
  403.  
  404. var emptyObject = {
  405. title: '',
  406. date: '',
  407. complete:''
  408. }
  409.  
  410. var emptyObject = {};
  411.  
  412. emptyObject.title = '객체와 함수 학습';
  413. emptyObject.date = '2017-06-17';
  414. emptyObject.complete = false;
  415.  
  416. console.log(emptyObject);
  417. **/
  418.  
  419.  
  420.  
  421. /*
  422.  
  423. 객체: 순서가 없는 값들의 집합, 배열: 순서가 있는 값들의 집합
  424.  
  425.  
  426. var emptyObjece = {};
  427.  
  428. var literalObject = {
  429. name1: 10,
  430. name2: 'Value',
  431. name3: true
  432. };
  433.  
  434. console.log(emptyObjece);
  435. console.log(literalObject);
  436. **/
  437. </script>
  438.  
  439.  
  440.  
  441. <script id="jsbin-source-javascript" type="text/javascript">/**
  442. 인수로서의 객체와 함수 기본 2
  443. 이벤트를 이용한 Todo 앱 개선
  444.  
  445.  
  446. var todos = [];
  447.  
  448. function newTodo(event) {
  449. let $ul = $('#todo-list');
  450. let $title = $('#title');
  451. let todosHTML;
  452.  
  453. // val:입력타입관계없이 호출
  454. todos.push($title.val());
  455.  
  456. // 입력후 비워준다.
  457. $title.val('');
  458. todosHTML = '';
  459.  
  460. for(let i=0; i < todos.length; i++) {
  461. todosHTML = `${todosHTML}<li>${todos[i]}</li>`;
  462. }
  463.  
  464. $ul.html(todosHTML);
  465. }
  466.  
  467. function app() {
  468. var $newTodo = $('#new-todo');
  469. //on:addeventListener
  470. $newTodo.on('click', newTodo);
  471. }
  472.  
  473. // onload시 제이쿼리가 괄호안에 함수를 호출한다.
  474. $(app);
  475. **/
  476.  
  477. /**
  478. 인수로서의 객체와 함수 기본 2
  479. 함수 인수 다루기 - 실행의 위임
  480. 언제일어날지 알수없을때,,,
  481.  
  482. function add(a, b, fn) {
  483. let result;
  484.  
  485. result = a + b;
  486.  
  487. fn(result);
  488. }
  489.  
  490. function finishAdd(sum) {
  491. console.log(sum);
  492. }
  493.  
  494. //콜백함수
  495. // 콜백함수의 예 setTimeout(함수명, 2000) 2초뒤 함수호출
  496. add(100, 200, finishAdd);
  497.  
  498. //위와동일
  499. add(100, 200, function(sum) {
  500. console.log(sum);
  501. });
  502.  
  503. add(100, 200, sum => console.log(sum));
  504. **/
  505.  
  506.  
  507.  
  508. /**
  509. 인수로서의 객체와 함수 기본 1
  510. 객체 인수
  511.  
  512. var todo = {
  513. items: [],
  514. status: {
  515. allCount: 0,
  516. completeCount: 0
  517. },
  518.  
  519. //겟터: 뺄때
  520. get allCount() {
  521. return this.status.allCount;
  522. },
  523.  
  524. // 셋터: 안에서는 함수지만 밖에선 속성,
  525. set allCount(count) {
  526. console.warn('임의로 할 일 개수를 설정할 수 없습니다, addItem 기능을 사용해 주십시오.');
  527. },
  528.  
  529. // 2번째 값이 없으면 옵션 { }안의 값을 default로 갖는다.
  530. addItem: function(title, options = {}) {
  531. let newTodo = {
  532. title: title
  533. };
  534.  
  535.  
  536.  
  537. newTodo.date = !!options.date ? options.date : '2017-06-17';
  538. newTodo.complete = !!options.complete;
  539.  
  540. this.items.push(newTodo);
  541.  
  542. this.status.allCount++;
  543.  
  544. if (newTodo.complete) {
  545. this.incrementCompleteTodo();
  546. }
  547. },
  548.  
  549. completeTodo(index) {
  550. this.items[index].complete = true;
  551. this.incrementCompleteTodo();
  552. },
  553.  
  554. incrementCompleteTodo() {
  555. this.status.completeCount++;
  556. }
  557. };
  558.  
  559. todo.addItem('객체와 함수 학습', {
  560. date: '2017-06-17'
  561. });
  562.  
  563. todo.addItem('객체 내의 객체 학습');
  564. todo.addItem('객체 인수 다루기', {
  565. complete: true
  566. });
  567.  
  568. **/
  569.  
  570. /**
  571. 인수로서의 객체와 함수 기본 1
  572. 객체 인수
  573.  
  574. var todo = {
  575. items: [],
  576. status: {
  577. allCount: 0,
  578. completeCount: 0
  579. },
  580.  
  581. addItem: function(title, options) {
  582. let newTodo = {
  583. title: title
  584. };
  585.  
  586. if (options) {
  587. if (options.date) {
  588. newTodo.date = options.date;
  589. } else {
  590. newTodo.date = '2017-06-17';
  591. }
  592.  
  593. newTodo.complete = options.complete ? options.complete : false;
  594. // newTodo.complete = !!options.complete;
  595. // newTodo.complete = option.complete || false;
  596. }
  597.  
  598. this.items.push(newTodo);
  599.  
  600. this.status.allCount++;
  601.  
  602. if (newTodo.complete) {
  603. this.status.completeCount++;
  604. }
  605. },
  606.  
  607. completeTodo(index) {
  608. this.items[index].complete = true;
  609. this.status.completeCount++;
  610. }
  611. };
  612.  
  613. todo.addItem('객체와 함수 학습', {
  614. date: '2017-06-17'
  615. });
  616.  
  617. todo.addItem('객체 내의 객체 학습');
  618. todo.addItem('객체 인수 다루기', {
  619. complete: true
  620. });
  621.  
  622. console.log(todo.status);
  623. **/
  624.  
  625.  
  626.  
  627. /**
  628. 인수로서의 객체와 함수 기본 1
  629. 가변 인수 다루기
  630. **/
  631. function add1(a, b) {
  632. return a + b;
  633. }
  634.  
  635. //유사배열
  636. function add2() {
  637. return arguments[0] + arguments[1];
  638. }
  639.  
  640. //최근추가된 스팩, ...가변인수
  641. function add3(...args) {
  642. return args[0] + args[1];
  643. }
  644.  
  645. // 명확하지않은 인수의 오동작을 방지한다
  646. function add21() {
  647. let result = 0;
  648.  
  649. for(let i=0; i < arguments.length; i++) {
  650. result += arguments[i];
  651. }
  652.  
  653. return result;
  654. }
  655.  
  656. //위와같음.
  657. function add31(...args) {
  658. let result = 0;
  659.  
  660. for(let i=0; i < args.length; i++) {
  661. result += args[i];
  662. }
  663.  
  664. return result;
  665. }
  666.  
  667. //arguments와 동시사용가능
  668. function add32(a, ...args) {
  669. let result = a;
  670.  
  671. for(let i=0; i < args.length; i++) {
  672. result += args[i];
  673. }
  674.  
  675. return result;
  676. }
  677.  
  678.  
  679.  
  680.  
  681. /**
  682. 객체의 속성과 메소드
  683.  
  684. var todo = {
  685. items: [],
  686. status: {
  687. allCount: 0,
  688. completeCount: 0
  689. },
  690.  
  691. addItem: function(title, date) {
  692. this.items.push({
  693. title: title,
  694. date: date,
  695. complete: false
  696. });
  697.  
  698. this.status.allCount++;
  699. },
  700.  
  701. //축약형. 함수. 객체안에서만 쓸수있다
  702. completeTodo(index) {
  703. this.items[index].complete = true;
  704. this.status.completeCount++;
  705. }
  706. };
  707.  
  708. todo.addItem('객체와 함수 학습', '2017-06-17');
  709. todo.addItem('객체 내의 객체 학습', '2017-06-17');
  710.  
  711. todo.completeTodo(1);
  712.  
  713. console.log(todo.status);
  714.  
  715. **/
  716.  
  717.  
  718.  
  719. /**
  720. 함수 내의 변수
  721. var 대신 let을써도됨(scope와 연관)
  722. 전역객체: window라는 객체아래생성, window.은 생략가능.
  723.  
  724. Let은 가장 가까운 블럭을 찾고, var는 가장가까운 함수를 찾는다..
  725.  
  726. function add100(a) {
  727. let x = 100;
  728.  
  729. return a+x;
  730. }
  731.  
  732. var result = add100(400);
  733.  
  734. console.log(result);
  735. console.log(x);
  736. **/
  737.  
  738.  
  739.  
  740.  
  741. /**
  742. 함수의 기본 형태 4가지
  743.  
  744. var result;
  745.  
  746. function add1(a, b) {
  747. return a + b;
  748. }
  749.  
  750. var add2 = function(a, b) {
  751. return a + b;
  752. }
  753.  
  754. // => function과 같은기능. 한줄함수(한줄이상도가능하나 한줄이상이면 return써야함)
  755. // => 다음에 나오는것을 값을인식, 자동return
  756. // 최근브라우저는 모두지원 ie10이하는 지원안함. 난해함이없어 다루기쉽다.
  757. var add3 = (a, b) => a + b;
  758.  
  759.  
  760.  
  761.  
  762. result = add1(10, 20);
  763. result = add2(10, 20);
  764. result = add3(10, 20);
  765.  
  766. //이름없는함수를 즉시실행할때..
  767. result = (function(a, b) {
  768. return a + b;
  769. })(10, 20);
  770.  
  771. //위와동일. arrow함수로 변형
  772. result = ((a, b) => a + b)(10, 20);
  773.  
  774. **/
  775.  
  776.  
  777. /*
  778. push:추가한다.
  779.  
  780.  
  781. var todo = {
  782. items: [],
  783. status: {
  784. allCount: 0,
  785. completeCount:0
  786. }
  787. };
  788.  
  789.  
  790. // 원래동일한말,, 해석하기쉽게 todo.items.length == todo.status.allCount;
  791.  
  792.  
  793. todo.items.push ({
  794. title: 'aaa',
  795. date: '',
  796. complete: false
  797. });
  798.  
  799. todo.status.allCount++;
  800.  
  801. */
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811. /* .을이용해 단계별접근. 건너띄고 바로접근안됨
  812. var somthingObject = {
  813. name: 'something',
  814. x: {
  815. x1: {
  816.  
  817. }
  818.  
  819. }
  820. };
  821.  
  822. somethingObject.x.x1
  823.  
  824. */
  825.  
  826.  
  827.  
  828.  
  829. /**
  830. + 객체 속성 추가하기 - 동적 생성
  831. 객체뒤 .을 이용해 객체추가생성, 위아래동일하다,,
  832.  
  833. var emptyObject = {
  834. title: '',
  835. date: '',
  836. complete:''
  837. }
  838.  
  839. var emptyObject = {};
  840.  
  841. emptyObject.title = '객체와 함수 학습';
  842. emptyObject.date = '2017-06-17';
  843. emptyObject.complete = false;
  844.  
  845. console.log(emptyObject);
  846. **/
  847.  
  848.  
  849.  
  850. /*
  851.  
  852. 객체: 순서가 없는 값들의 집합, 배열: 순서가 있는 값들의 집합
  853.  
  854.  
  855. var emptyObjece = {};
  856.  
  857. var literalObject = {
  858. name1: 10,
  859. name2: 'Value',
  860. name3: true
  861. };
  862.  
  863. console.log(emptyObjece);
  864. console.log(literalObject);
  865. **/
  866.  
  867. </script></body>
  868. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement