Tuesday, 25 April 2017

ECS6 How define/create new an Object inside a Class

I'm beginner in em ES6, and I need instantiate new objects inside a class. How the best way to make this, and how I can access "in this case" the connection in the function getName().

Probably talking nonsense, but I need to understand how this works to evolve. Anyone would have any tips for me.

above little exemple

class Test {
  constructor(array){
    this.array = array

    // CONNECT DATABASE
    var connection = mysql.createConnection({this.array})

  }
  getName(){
    query = 'SELECT * FROM mytable LIMIT 1'
    connection.query(query, function (err, rows, fields) {
      return rows
    })
  }
}


array = {
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'mydb'
}

let T = new Test(array)

T.getName()
// error connection is not defined



via Henrique Van Klaveren

No comments:

Post a Comment