I couldn’t understand the new cpaint()

Asked

Viewed 132 times

1

The code below I don’t 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 kind of var cp?

  • Take a look at this link

  • 1

    The guy will be object. JS is not C#, so there are few types. Have some more specific doubt or is this all you want to know?

  • @bigown, what I couldn’t really understand was cp.call. What function or something it’s calling? Because of what I’m doing here, I was flying.

  • @Jéfersonbueno, okay, when you get home if you don’t find an answer here, I’ll take a look. Here in the company this link is blocked by security policy.

  • @Cesarmiguel, this is normal

2 answers

3


I’m not sure if it answers the question, but it should clarify how the keyword works new.

This is the way to simulate object orientation in javascript. The variable type 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"

Observing: Inside the example libraries above this refers to the context created with new, call the function without new will not generate the desired behavior.

1

I’ll try to answer your two questions.

Whenever you have questions, check the framework documentation. You said the following "what I couldn’t really understand was cp.call" here’s the link to the entire description of what the call method does http://cpaint.sourceforge.net/doc/frontend.class.cpaint.call.html.

As for doubt "What type of var cp"

In javascript (as already said) there are few reserved types. It is quite different from compiled languages like C#, Java and etc.

So the type of var cp 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 within the variable meucarro there is an instance of Car. In other words, the variable meucarro has the same behaviors as Car, being possible to make use of all its methods and operations.

I hope I helped you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.