Posts Tagged ‘JavaScript’

javascript arguments

September 29, 2013

To determine how many arguments were passed to a function?

Check the length, of the array like arguments variable, arguments.length.

To determine, how many variables, a function expects? 
Write the name of the function, and retrieve its length property.

	function sayHi() {
		console.log(arguments.length);
		for
		(
			var i=0, argumentsLength = arguments.length; 
			i < argumentsLength;
			++i
		) 
		{
			console.log(arguments[i])
		}
	}

	sayHi("Cat", "Alice")

	//To determine how many arguments a function expects
	console.log(sayHi.length);

http://javascript.info/tutorial/arguments

javascript type

September 26, 2013

To determine the type of an unevaluated operand

typeof undefined === "undefined"

typeof null === “object”

typeof true === “boolean”

typeof 3.14 === “number”

typeof “good” === “string”

typeof Infinity === “number”

typeof NaN === “number”

typeof new Date() === “object”

typeof {a:1} === ‘object’

typeof [1, 2, 4] === ‘object’; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays

To differentiate between the various typeof objects

var toClass = {}.toString
console.log( toClass.call( [1, 2] ) );

To determine if an object, is an instance of a class

function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car("Honda", "Accord", 1998);
var a = mycar instanceof Car; // returns true
var b = mycar instanceof Object; // returns true

Reference

http://javascript.info

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Working together – Node.js and mysql

July 17, 2012

This article assumes that Node.js and mysql are installed.

Type in, the command below, to install the mysql module.

npm install mysql@2.0.0-alpha3

Listed below, is a sample program, mysql.js, for connecting to mysql.

var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'user',
  password : 'secret',
  database : 'database name'
});

connection.connect();

connection.query('SELECT * from contact', function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);

  var columns=new Array(fields.length);
  for (var j = 0; j < fields.length; ++j) {
	columns[j] = fields[j]['name'];
  }

  for (var i in rows) {
	var row = rows[i];
	//console.log(row.FirstName);
	for (var j = 0; j < fields.length; ++j) {
		console.log(columns[j], ': ', row[columns[j]]);
	}	
  }

});

//Escaping query values
var contactId = 1;
var sql    = 'SELECT * FROM Contact WHERE ContactID = ' + connection.escape(contactId);
connection.query(sql, function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);
});

//Escaping query values  
var contactId = 2;
var sql    = 'SELECT * FROM Contact WHERE Contactid = ?';
connection.query(sql, [contactId], function(err, rows, fields) {
  if (err) throw err;

  console.log("Number of rows: " + rows.length);
  console.log('Query result: ', rows);
});

//Insert
var post  = {Dated: new Date(), FirstName: 'Jane', LastName: 'Smith'};
var query = connection.query('INSERT INTO Contact SET ?', post, function(err, rows, fields) {
  if (err) throw err;
  console.log(query.sql); 
  console.log(rows.insertId);
});

connection.end();

References

felixge / node-mysql