Popularni postovi

Wednesday, 2 July 2014

JavaScript Tips



Function tips
  • Declare local variables at the top of the body of your function.
  • ƒƒIf you forget to declare a local variable using var, that variable will be global, which could have unintended consequences in your program.
  • A function can optionally return a value using the return statement.
  • Variables are either in the global scope (visible everywhere in your program) or in the local scope (visible only in the function where they are declared). 
  • If variable in function is the same name as global variable, function variable will be active in that function.
  • There are two ways to define a function: with a function declaration and with a function expression.
  • A function reference is a value that refers to a function.
  • Function declarations are handled before your code is evaluated. Declaration example: function play(sequence) {// code here}
  • Function expressions are evaluated at runtime with the rest of your code. Those functions you can use as variable, pass like parameter etc... Declaration example:  var pause = function() {//code here}
  • Closure example: function makeCounter() { var count = 0; function counter() {count = count + 1; return count;return counter; } - Function makeCounter returns as result subfunction counter and counter function uses count variable from makeCounter function, and that variable is not accesable by others. Calling counter, variable count will be rised by one.
    var doCount = makeCounter(); console.log(doCount()); console.log(doCount()); 
    This code will display: 1 2 3 
Array tips
  • A sparse array is just an array that has values at only a few indices and no values in between. You can create a sparse array easily, like this: var sparseArray = [ ]; sparseArray[0] = true; spraseArray[100] = true; In this example, the sparseArray has only two values, both true, at indices 0 and 100.The values at all the other indices are undefined. The length of the array is 101 even though there are only two values.
  • You can access any item using its index.For example, use myArray[1] to access item one (the second item in the array).
  • Assigning a value to an existing item will change its value. Assigning a value to an item that doesn’t exist in the array will create a new item in the array.
  • You can use a value of any type for an array item. 
  • Not all the values in an array need to be the same type.
  • You can create an empty array with var myArray = [ ];
  • You can add a new value to an array using push.
Objects tips

  • Object without any properties ca be created: var lookNoProps = { }; because they can be added dynamicly! Example: var lookMaNoProps = { }; lookMaNoProps.age = 10;
  • Variables don’t actually hold objects. 
  • Instead they hold a reference to an object.
  • Any changes you make to the object inside a function will still be there when the function 
  • completes.
  • Use this with dot in child functinos in objects to tell JavaScript that you mean the property of THIS object.
  • In child functions od objects and functions, this is referencing on parent object or function, but in parent(which is not child of any) function this is refferening on GLOBAL scope!!!
  • Fuctions in objects are colled methods.
  • You can also delete properties from objects dynamicly, using the delete operator.
  • To know if an object was createdby a specific constructor, use the instanceof operator.
  • Use the new operator with a constructor function call to create an object.
  • Use this in a constructor function to access the object being constructed and add properties to the object.



No comments:

Post a Comment