Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

354
Vistas
Comprobar si un valor no se ha inicializado siempre devuelve que el valor no se ha inicializado, incluso si lo está.

Estoy intentando un script similar a jQuery que admita selectores, mostrar, ocultar, texto y métodos html. Al ejecutar una prueba, independientemente del método al que llame, siempre arroja un error personalizado "No se seleccionó ningún elemento". ¿Cómo puedo solucionar esto? El código completo está aquí --> https://jsfiddle.net/7ado13vg/

El código relevante está debajo

 var storingAttributes = {}; class MONEY { constructer(string) { try { //check for action if (string != null) { //Check Selection Type storingAttributes.selectorArray = string.split('') if (storingAttributes.selectorArray[0] == '#') { storingAttributes.selectionType = 'id'; storingAttributes.selectorArray.splice(0, 1); } if (storingAttributes.selectorArray[0] == '.') { storingAttributes.selectionType = 'class'; storingAttributes.selectorArray.splice(0, 1); } if (storingAttributes.selectorArray[0] != '#' && storingAttributes.selectorArray[0] != '.') { storingAttributes.selectionType = 'tag'; } //Select Elem if (storingAttributes.selectionType == 'id') { storingAttributes.selectedElem = document.querySelector(string); } if (storingAttributes.selectionType == 'class') { storingAttributes.selectedElem = document.querySelectorAll(string); } if (storingAttributes.selectionType == 'tag') { storingAttributes.selectedElem = document.querySelectorAll(string); } } } catch (error) { } } //The reason I checked what was being selected is because querySelectorAll returns an array, and I do not believe that ids are supported //trying to use my method 'show' show() { try { if (typeof storingAttributes.selectedElem == 'undefined' || storingAttributes.selectedElem == null) { throw "No Element Selected"; } else { if (storingAttributes.selectionType == 'tag') { for (n = 0; n < storingAttributes.selectedElem.length; ++n) { storingAttributes.selectedElem[n].style.display = 'block'; } return; } if (storingAttributes.selectionType == 'class') { for (n = 0; n < storingAttributes.selectedElem.length; ++n) { storingAttributes.selectedElem[n].style.display = 'block'; } return; } if (storingAttributes.selectionType == 'id') { storingAttributes.selectedElem.style.display = 'block'; return; } } } catch (e) { console.log(e); } } }

Como puede ver, el constructor simplemente asigna/inicializa un valor para selectionType , selectedElem y selectorArray en el objeto storingAttributes

Cuando registras storingAttributes de almacenamiento, devuelve un objeto vacío.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Lo siguiente reemplaza el código que ha publicado para mostrar cuánto más simple se puede escribir y sin usar erróneamente try/catch .

Entiendo que no responde a su pregunta, pero el código con el que está trabajando es tan innecesariamente excesivo que es difícil filtrarlo para encontrar su problema sin refactorizarlo. El código más simple es más fácil de depurar.

 var storingAttributes = {}; // Just test for the non-existance of a value if (!storingAttributes.selectedElem) { throw "No Element Selected"; } else { // switch is more concise when you have a single value to // check against multiple possible values switch (storingAttributes.selectionType){ case "tag": // And since you want to do the exact same code if // it's "tag" or "class", we'll allow fall through here case "class": // The Array.prototype.forEach method makes looping much simpler storingAttributes.selectedElem.forEach(function(item){ item.classList.add("block"); }); break; case "id": storingAttributes.selectedElem.classList.add("block"); } }
 /* Avoid inline styles which lead to duplication of code. Instead, use CSS classes where possible. */ .block { display:block; }

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda