Advertisement
Guest User

Untitled

a guest
Aug 7th, 2018
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | None | 0 0
  1. script.js
  2.  
  3. /*
  4. Tipos de datos
  5.  
  6. Primitivos
  7. -String : "" '' ``
  8. -Number : 1 1.5 -3
  9. -Boolean : {true|any} ó {false|null|undefined|0|""}
  10. -undefined : Una variable sin valor ó una funcion sin retorno
  11. -null
  12.  
  13.  
  14. Objetos : matrices indexadas asociativas
  15. -Object : {x:1}
  16. -Array : [1,2,3]
  17. -Function : function foo(){} foo() foo.x = 1
  18. -Date
  19. -Math
  20. -Console
  21. -XMLHttpRequest
  22. */
  23.  
  24. //Pasa por valor
  25. var a = 1
  26. var b = a
  27. a = 10
  28. //Pasa por referencia
  29. var c = {x:1}
  30. var d = c
  31. c.x = 10
  32. /*
  33. var - let - const
  34. */
  35. //redeclaracion
  36. var a = 10
  37. var a = 20
  38. //redefinir
  39. var b = 10
  40. b = 30
  41. //no permanece en scope
  42. if(true){
  43. var y = 10
  44. console.log(y)
  45. }
  46. console.log(y)
  47. //Le pertenece al objeto global
  48.  
  49.  
  50. //No admite redeclaracion
  51. let f = 10
  52. //let f = 20
  53. //redefinicion
  54. f = 50
  55. //admite scope de cualquier bloque
  56. if(true){
  57. let x = 10
  58. console.log(x)
  59. }
  60. //console.log(x)
  61. //es parcialmente global
  62.  
  63. //no admite redeclaracion
  64. const h = 10
  65. //const h = 10
  66. //no admite redefinicion total / si admite parcial
  67. //h = 20
  68. const i = {x:1}
  69. i.x = 10
  70. //admite scope de cualquier bloque
  71. if(true){
  72. const z = 10
  73. console.log(z)
  74. }
  75. //console.log(z)
  76.  
  77. function foo(){
  78. var a = 10
  79. }
  80. //variable funcional
  81. var foo = function(){}
  82. window.foo = function(){}
  83.  
  84.  
  85. /*
  86. Notaciones
  87.  
  88.  
  89. punto
  90.  
  91. */
  92. //corchete
  93. var arr = [1,2,3]
  94. var indice = 1
  95. //Indices numericos
  96. arr[0]//1
  97. //Indices string
  98. arr["length"]
  99. //Indices variables
  100. arr[indice]//2
  101. //punto
  102. //Indices numericos
  103. //arr.0//Unexpected Number
  104. //indices string
  105. arr.length
  106. //indices variables
  107. arr.indice//undefined
  108.  
  109. console.clear()
  110.  
  111. //JSON : Javascript Object Notation
  112. //BOM Browser Object Model
  113. /*
  114.  
  115. innerHeight/innerWidth : medidas del HTML
  116. outerHeigth/outerWidth : medidas del navegador
  117. location.href : barra de navegacion
  118. history.back/forward/go(N) : controla el historial
  119.  
  120.  
  121. */
  122.  
  123. function uno(){dos()}
  124. function dos(){tres()}
  125. function tres(){cuatro()}
  126. function cuatro(){console.error("error")}
  127. uno()
  128.  
  129. console.time("contador")
  130. for (var k = 0; k < 100000; k++) {
  131. continue;
  132. }
  133. console.timeEnd("contador")
  134.  
  135. console.table([{x:1,y:2},{x:1,y:2},{x:1,y:2}])
  136.  
  137. console.clear()
  138.  
  139. console.dir(document)
  140. //DOM : Document Object Model
  141. //Seleccionar nodos
  142. /*
  143.  
  144. let h1 = document.getElementById('titulo') => Node
  145. let h1 = document.getElementByClassName('titulo') => Array
  146. let h1 = document.getElementsByTagName('h1') => Array
  147.  
  148. let h1 = document|Node.querySelector('') => Node
  149. let h1 = document|Node.querySelectorAll('') => Array
  150.  
  151. */
  152.  
  153. let lista = document.querySelector("ul")
  154. let items = lista.querySelectorAll("li")
  155.  
  156.  
  157. //Editar nodos
  158. let h1 = document.getElementById('titulo')
  159. /*
  160. h1.getAttribute("id")
  161. h1.setAttribute("id","nuevoid")
  162. */
  163. console.log(h1.id)
  164. h1.id = "nuevoid"
  165.  
  166.  
  167.  
  168. //Crear nodos
  169. //document.createElement(String any) => HTMLElement | Unknown Element
  170. let p = document.createElement("p")
  171. p.id = "parrafo"
  172. p.innerText = "lorem Ipsum"
  173. let body = document.querySelector("body")
  174. body.appendChild(p)
  175. //Node.appendChild(Node)
  176. lista.appendChild(p )
  177. //Borrar nodos
  178. //body.removeChild(p)
  179. /*
  180. Parent.insertBefore(New,Ref)
  181. */
  182.  
  183. let lista_nueva = document.getElementById("lista")
  184. let fragmentos = document.createDocumentFragment()
  185.  
  186. for (var l = 0; l < 5; l++) {
  187. let li = document.createElement("li")
  188. li.innerText = `Item ${l}`
  189. fragmentos.appendChild(li)
  190. }
  191.  
  192. lista_nueva.appendChild(fragmentos)
  193.  
  194. //Agregar un nodo - remover un nodo - cambiar el id , texto , clase , estilos
  195. //Reflow/Relayout : Calculo de tamaño de cada etiqueta
  196. //Repaint : Volver a pintar el CSS
  197.  
  198.  
  199. /*
  200. Eventos : La ejecucion de una funcion(callback) como respuesta a una accion
  201. */
  202. //onclick="console.log('click')"
  203. let btn = document.querySelector("button")
  204. //btn.onclick = console.log('click')
  205. /*btn.onclick = function(){
  206. console.log("click")
  207. }
  208.  
  209. btn.onclick = hacerClick
  210.  
  211. function hacerClick(){
  212. console.log("click hacer click")
  213. }*/
  214.  
  215. //Nodo.addEventListener(String tipo,Function callback[,Boolean fase])
  216. btn.addEventListener("click",function(){
  217. console.log("click 1")
  218. })
  219.  
  220. btn.addEventListener("click",function(){
  221. console.log("click 2")
  222. })
  223.  
  224. window.addEventListener("re",function(){
  225. console.log("click 3")
  226. })
  227.  
  228. /*
  229. El objeto global window tiene un evento llamado resize que se ejecuta cada vez que el navegador cambia su tamaño.
  230. Si el ancho esta a menos de 500px , cambiar el color de fondo del body por rojo o azul si estoy a mas de esa medida
  231. */
  232.  
  233. btn.addEventListener("",function(){
  234. console.log("click 1")
  235. })
  236.  
  237.  
  238.  
  239. let bodyy = document.querySelector("body")
  240.  
  241. window.addEventListener("resize",cambiarColor)
  242.  
  243. function cambiarColor(){
  244. if(innerWidth<500){
  245. bodyy.style.backgroundColor = "red"
  246. }else{
  247. bodyy.style.backgroundColor = "blue"
  248. }
  249. }
  250.  
  251.  
  252. let usuarios = [{"nombre":"Arturo","apellido":"Villa","email":"Lucia77@hotmail.com","usuario":"Vicente21","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/bcrad/128.jpg","album":["http://lorempixel.com/100/100/nightlife","http://lorempixel.com/100/100/nightlife","http://lorempixel.com/100/100/abstract"]},{"nombre":"Manuela","apellido":"Zamudio","email":"Guillermina_Bahena@gmail.com","usuario":"Inés_Alaniz1","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/chrisstumph/128.jpg","album":["http://lorempixel.com/100/100/transport","http://lorempixel.com/100/100/cats","http://lorempixel.com/100/100/technics"]},{"nombre":"Mariano","apellido":"Avilés","email":"Emilio47@yahoo.com","usuario":"Lucas.Maya","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/joannefournier/128.jpg","album":["http://lorempixel.com/100/100/abstract","http://lorempixel.com/100/100/transport","http://lorempixel.com/100/100/sports"]},{"nombre":"Samuel","apellido":"Escobar","email":"David.Borrego94@gmail.com","usuario":"Octavio.Atencio14","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/hjartstrorn/128.jpg","album":["http://lorempixel.com/100/100/technics","http://lorempixel.com/100/100/nature","http://lorempixel.com/100/100/city"]},{"nombre":"Salvador","apellido":"Nava","email":"Gilberto.Nava@hotmail.com","usuario":"Emilio85","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/keyuri85/128.jpg","album":["http://lorempixel.com/100/100/transport","http://lorempixel.com/100/100/people","http://lorempixel.com/100/100/sports"]}]
  253.  
  254. for (var l = 0; l < usuarios.length; l++) {
  255. let divusuarios = document.createElement("div")
  256. let imgusuarios = document.createElement("img")
  257. let h3usuarios = document.createElement("h3")
  258. let pusuarios = document.createElement("p")
  259. let spanusuarios = document.createElement("span")
  260. divusuarios.class = "usuario"
  261. divusuarios.id = l;
  262.  
  263. imgusuarios.src = usuarios[l].avatar;
  264.  
  265. h3usuarios.innerText = "Mi" +usuarios[l].nombre+ "y" + usuarios[l].apellido + usuarios[l].usuario
  266. pusuarios.innerText= usuarios[l].email
  267.  
  268.  
  269. let figureusuarios = document.createElement("figure")
  270. figureusuarios.class = "album"
  271.  
  272.  
  273. for (var o = 0; o< usuarios[l].album[o].length; o++) {
  274. let img1= figureusuarios.createElement("img")
  275. img1.src = usuarios[l].album[o];
  276. figureusuarios.appendChild(img1)
  277. }
  278. body.appendChild(h3usuarios)
  279. body.appendChild(divusuarios)
  280. body.appendChild(imgusuarios)
  281. body.appendChild(pusuarios)
  282. body.appendChild(figureusuarios)
  283. }
  284.  
  285. ejemplo profesor:
  286. for (var d = 0; d < personas.length ; d++) {
  287.  
  288. let cuerpo = document.createElement("div")
  289. let avatar = document.createElement("img")
  290. let nombre = document.createElement("h3")
  291. let span = document.createElement("span")
  292. let email = document.createElement("p")
  293. let album = document.createElement("figure")
  294.  
  295. cuerpo.className = "usuario"
  296. cuerpo.id = d
  297. avatar.src = personas[d].avatar
  298. nombre.innerText = `${personas[d].nombre} ${personas[d].apellido}`
  299. span.innerText = personas[d].usuario
  300. email.innerText = personas[d].email
  301.  
  302. nombre.appendChild(span)
  303.  
  304. cuerpo.appendChild(avatar)
  305. cuerpo.appendChild(nombre)
  306. cuerpo.appendChild(email)
  307. cuerpo.appendChild(album)
  308. fragmentos.appendChild(cuerpo)
  309. }
  310. lista.appendChild(fragmentos)
  311.  
  312. /*
  313. Bubbling : Dispara el callback registrado del mismo nodo y va subiendo por todos los nodos padres directo ejecutando el callback del mismo evento si tuviera uno registrado
  314.  
  315. Capturing : Opuesto a bubbling
  316. */
  317.  
  318. window.addEventListener("click",function(e){
  319. console.log("click 3")
  320. console.log(e)
  321. }/*,true*/)
  322.  
  323. let div_tres = document.getElementById("tres")
  324. div_tres.addEventListener("click",function(e){
  325. //e.stopPropagation()
  326. console.log("div 3")
  327. }/*,true*/)
  328.  
  329. let div_uno = document.getElementById("uno")
  330. div_uno.addEventListener("click",function(){
  331. console.log("div 1")
  332. }/*,true*/)
  333.  
  334.  
  335.  
  336. let title = document.createElement("h3")
  337. let btn1 = document.createElement("button")
  338. let btn2 = document.createElement("button")
  339. title.innerText = "esta seguro que quiere salir?"
  340.  
  341. btn1.innerText = "Aceptar"
  342.  
  343. btn2.innerText = "Cancelar"
  344.  
  345.  
  346. let aa = document.querySelector("a")
  347. aa.addEventListener("click",function(e){
  348.  
  349. e.stopPropagation()
  350. e.preventDefault()
  351.  
  352.  
  353. body.appendChild(btn1)
  354. body.appendChild(btn2)
  355. body.appendChild(title)
  356.  
  357.  
  358.  
  359. /*
  360. Mostrar en el DOM un mensaje de confirmacion("esta seguro que quiere salir?") y dos botones que digan aceptar y cancelar.
  361. Si hago click en aceptar cambio la url de la barra de navegacion por el href del link y si hago click en cancelar , borro todo el mensaje de confirmacion y los dos botones
  362. */
  363. })
  364.  
  365.  
  366.  
  367. btn1.addEventListener("click",function(){
  368. e.stopPropagation()
  369. window.location.href = aa.href;
  370. })
  371.  
  372. btn2.addEventListener("click",function(){
  373. e.stopPropagation()
  374. body.removeChild(title)
  375. body.removeChild(btn1)
  376. body.removeChild(btn2)
  377. })
  378.  
  379. //alert(1)
  380.  
  381. //ejemplo profesor
  382.  
  383. let aaa = document.querySelector("a")
  384. aaa.addEventListener("click",confirmacion)
  385.  
  386. let mensaje = document.createElement("span")
  387. let botonOk = document.createElement("button")
  388. let botonCancel = document.createElement("button")
  389. mensaje.innerText = "Confirmar accion"
  390. botonOk.innerText = "Ok"
  391. botonCancel.innerText = "Cancelar"
  392.  
  393. function confirmacion(e) {
  394. e.stopPropagation()
  395. e.preventDefault()
  396. body.appendChild(mensaje)
  397. body.appendChild(botonOk)
  398. body.appendChild(botonCancel)
  399. }
  400.  
  401. botonOk.addEventListener("click",redirigir)
  402. function redirigir(e){
  403. e.stopPropagation()
  404. location.href = aaa.href
  405. }
  406.  
  407.  
  408. botonCancel.addEventListener("click",cancel)
  409. function cancel(e){
  410. e.stopPropagation()
  411. body.removeChild(mensaje)
  412. body.removeChild(botonOk)
  413. body.removeChild(botonCancel)
  414. }
  415. //alert(1)
  416.  
  417. index.html
  418.  
  419. <!DOCTYPE html>
  420. <html lang="en">
  421. <head>
  422. <meta charset="UTF-8">
  423. <title>Document</title>
  424. <style>
  425. h1{
  426. color: red;
  427. }
  428. #uno{width: 200px;height: 200px;background: red;}
  429. #dos{width: 150px;height: 150px;background: green;}
  430. #tres{width: 100px;height: 100px;background: blue;}
  431. </style>
  432. </head>
  433. <body>
  434. <a href="http://google.com">google</a>
  435. <div id="uno">
  436. <div id="dos">
  437. <div id="tres"></div>
  438. </div>
  439. </div>
  440. <button >click</button>
  441. <h1 id="titulo" class="titulo">Titulo</h1>
  442. <ul>
  443. <li>link 1</li>
  444. <li>link 2</li>
  445. <li>link 3</li>
  446. </ul>
  447. <ul id="lista"></ul>
  448. <script src="script.js"></script>
  449. </body>
  450. </html>
  451.  
  452. link util
  453.  
  454. https://developer.mozilla.org/es/docs/Web/API
  455. alumni.educacionit.com
  456. Undefined: variable sin valor o una función sin retorno.
  457. Bool asd
  458. If(asd) –Si asd es false puede ser null undefined 0 etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement