I could not understand new cpaint ()

1

The code below did not understand what it means:

var cp = new cpaint();
    cp.set_response_type('text');
    cp.set_debug(false);
    cp.call('../../atd/asp/atd0027m_crossbrowser.asp', 'BuscarPrestadorNome', ExibePrestadorNome, sNomePrestador, sCodOperadora, sCodUnidade); 

What type of var cp ?

    
asked by anonymous 04.09.2015 / 18:10

2 answers

3

I'm not sure if you answer the question, but you should clarify how the new keyword works.

This is the way to simulate object orientation in javascript. The type of variable cp will be object due to new , but its content can be done in two ways.

One of the definitions returns an interface object, with public methods and attributes, and the other returns nothing, the code is self-explanatory, and the comments complement.

var lib_exemplo = function() { // não retorna nada
  var variavel_privada = 'foo';
  this.variavel_publica = 'bar'; // vinculada ao "this"
  this.funcao = function() { // pública
    return variavel_privada;
  }
}

ex1 = new lib_exemplo();
console.log(ex1.variavel_privada); // undefined
console.log(ex1.variavel_publica); // "bar"
console.log(ex1.funcao()); // "foo"

var lib_exemplo_2 = function() {
  var self = this; // variável para acesso interno
  var variavel_privada = 'foo';
  this.variavel_privada_2 = 'bar';
  this.variavel_privada_3 = 'foobar';
  // definições com "var" e "this" são privadas, muda a forma de acesso

  return { // retorna "interface" - métodos e atributos publicos
    variavel1: variavel_privada,
    variavel2: this.variavel_privada_2,
    funcao: function() {
      // "this" dentro na função na interface se refere à interface
      // então use "self"
      return self.variavel_privada_3;
    }
  }
}

ex2 = new lib_exemplo_2();
console.log(ex2.variavel_privada); // undefined
console.log(ex2.variavel_privada_2); // undefined
console.log(ex2.variavel1); // "foo"
console.log(ex2.variavel2); // "bar"
console.log(ex2.funcao()); // "foobar"

Note: Inside the sample libraries above this refers to the context created with new , calling the function without new will not generate the desired behavior.     

08.09.2015 / 14:33
1

I'll try to answer your two questions.

Whenever in doubt, consult the framework documentation. You said the following "what I could not really understand was cp.call" here is the link to the entire description of what the call method does link .

As for the doubt " What kind of var cp "

In javascript (as has been said) there are only a few reserved types. It is very different from compiled languages like C #, Java and etc.

Then the varcp type would be " object ", make a simple test to prove this.

function Carro() {
  var teste = "";
}

var meucarro = new Carro();

console.log(typeof meucarro);

The answer will be "object" and not Car (as it would be in many languages like java and C # for example).

But inside the variable meucarro there is an instance of Car. In other words, the variable meucarro has the same behaviors of Car, being possible to make use of all its methods and operations.

I hope I have helped you.

    
08.09.2015 / 14:53