JavaScript Quick Tips #2
04/28/2013, Sun
Categories:
#JavaScript
Concatenating when Logging without the Plus Sign
You can use the comma in place of the plus sign when concatenation is needed.
// Give the Comma a Try When Logging
var stuff = "my stuff";
// the comma acts like the plus to concatenate your strings
console.log("some text:", stuff);
Attaching Properties to Non-Object Objects
Usually when you deal with an array you normally do not attach properties to it, but you are allowed to do such a thing.
// Attaching Property to an Array
var myArray = [1, 2, 3];
myArray.aKey = "myValue";
// outputs "myValue"
console.log(myArray.akey);
You can also do the same thing for a function and other non-object objects.
// Now Attaching Property to a Function
var myFunction = function () {};
// outputs "myValue"
myFunction.aKey = "myValue";
The reason you can do this is that everything in JavaScript is treated like an object. You may want to do this to include metadata on your arrays, functions or other variable types that are not object objects, but do use your discretion when an object is more fitting for the task.