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

260
Vistas
How to correctly put function in object Javascript by method?

I want to make an object by function method because of lots of object I have to put in. I do step by step from instruction but this method do not work for me. I think the main problem is in function in function. I want to have all object have their its value of the second function so I name "A", "B", "C", "D" fields.

function pol () {
    this.name = name;
    this.moc = moc; 
    this.square = square;
    this.A = A;
    this.B = B;
    this.C = C;
    this.D = D;
    this.price = function(){
        if(wynik1 >18000){ return A;}
        if(wynik1 >10000){ return B;}
        if(wynik1 >5000){ return C;}
        if(wynik1>3000){ return D;}
    })();    
}

var Bingo = new pol ("Psyche", 370, 1.7, 4000, 4200, 4600, 5000); 
var Ringo = new pol("Psyche Myche", 370, 1.7, 4000, 4200, 4600, 5000); 
var Zingo = new pol("Psyche Ryche", 370, 1.7, 4000, 4200, 4600, 5000); 

var moc = pol.Ringo.moc
var pow = pol.Ringo.square
var prices = pol.Ringo.price();

I have problem in this step but my next step is

var moc = function (){
    if(k =="A"){return: pol.Ringo.moc}
    if(k =="A"){return: pol.Bingo.moc}
    if(k =="A"){return: pol.Zingo.moc}
}();

Is this possible?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Notice that when you run:

var Ringo = new pol("Psyche Myche", 370, 1.7, 4000, 4200, 4600, 5000); 

you have added inputs into this pol constructor. By that logic, you also need to have parameters defined in the pol constructor like:

function pol (name, moc, square, A, B, C, D) {...}

As a side note, be careful about the line: var prices = pol.Ringo.price(); You define this.price as:

function(){
    if(wynik1 >18000){ return A;}
    if(wynik1 >10000){ return B;}
    if(wynik1 >5000){ return C;}
    if(wynik1>3000){ return D;}
})();

Those final parentheses are making this function return a value instead of setting this.price to a function. You'll notice I copied the syntax error (the single parenthesis after the close of the function) because that decides whether this will return a value or not. If you remove that one parenthesis, you also need to remove the () as well. Decide if this.price is supposed to be a function or a value. Based on the fact that you tried pol.Ringo.price(), you probably want it to instead be a function like (also note the added parameter of wynik1 (assuming you then call Ringo.price(some number)):

this.price = function(wynik1) {
    if(wynik1 >18000){ return A;}
    if(wynik1 >10000){ return B;}
    if(wynik1 >5000){ return C;}
    if(wynik1>3000){ return D;}
}; // no additional parentheses

The this.price function should also not be using A, B, C, and D. It should instead be accessing the specific values that are connected with a specific pol constructor: this.A, this.B, this.C, and this.D.

The final note is that the use of pol.Ringo.moc (or any of your other parameters will not work. Since pol is a constructor, when you create a new pol, there is no connection to the variable pol. Ringo will now point to different storage that has all of the information copied over, then set based on the parameters you get to new pol(...). You instead can access any of your new pol constructs just by using the variable name and doing something like:

var moc = Ringo.moc
var pow = Ringo.square
var prices = Ringo.price();
about 4 years ago · Juan Pablo Isaza Denunciar

0

finally, with your help, I managed to fix it but I don't understand why it works.

var Ringo = new pol("Psyche", 370, 1.7, 4000, 4200, 4600, 5000); 
var prices = Ringo.price(4000, 4200, 4600, 5000);

in Ringo.price(); I can put any numbers and it works just like 1, 2, 3, 4 but if I put "a", "b", "c', "d" or empty do not. Why i can not just put there noting.

about 4 years ago · Juan Pablo Isaza 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