Monday, April 27, 2015

Amazing things about JavaScript

arguments object:
arguments is a default object which is always there for you even when you haven't passed anything to your function. you still have arguments object. and that's the reason why you can send any number of parameters to a JavaScript function with any number of arguments.

For example you can write the code below :

var sum = function(){
var sum=0;
sum += arguments[i]; 



Immediate function execution expressions are the functions which are executed as soon as they are declared.


There are many ways to create objects in Javascript

Literal Object Notation:
Yup this is so similar to Json because json is subset of Literal Object Notation :)

var obj = {
  prop1: value1,
  prop2: value2

Object oriented JavaScript:  JavaScript is object oriented but in different ways than regular programming languages. An object in JavaScript can be initialized in the many ways. some are below

Using Constructor functions:
  • Constructor functions have one major difference that they members are accessed by using this keyword.
  • There name usually starts with an upper case letter.
  • after you create a constructor function all you have to do is to use "new" keyword to create object of that function
Using base Object function:
var obj = new Object();
obj.a= 'val';
obj.b= 'val2';

Using object.create Function:
using obect.Create is supported in major browsers but not in IE6 or 8

var a = Object.create(null, { a: { value: 1 }, b: { value: 2 }}); // no prototype
var b = Object.create({}, { a: { value: "a" }, b: { value: "b" }}); // prototype is an empty obj
var c = Object.create({}, { a: { value: true }, b: { value: false }}); // prototype is an empty obj

console.log(a.a); // outputs 1
console.log(a.b); // outputs 2
console.log(b.a); // outputs a
console.log(b.b); // outputs b
console.log(c.a); // outputs true
console.log(c.b); // outputs false

further read: 
Recommended Books

No comments:

Post a Comment