-
-
Save SanthoshVijayabaskar/a49470f4cced0c4582b2 to your computer and use it in GitHub Desktop.
![func_examples](https://cloud.githubusercontent.com/assets/1716894/11166646/fe53bf2c-8b63-11e5-84bd-aed863198b6f.jpg)
- Objects always use By reference (even when passed as parameters)
- Equal operator sets up a new memory space (new address)
- All Primitive types uses By Value
console.log('Hello '+ name);
}('John');
var greeting = function(name){
return 'Hello '+ name;
}('John');
console.log(greeting);
var firstname = 'John';
(function(global,name){
var greeting = 'Inside IIFE: Hello';
global.greeting = 'Hello';
console.log(greeting + ' ' + name);
}(window,firstname)); //IIFE - Also you can use the params inside the outer parantheses or outside the outer parantheses
var person = new Object(); // Creates an Person Object
We can use the '{ }' braces to create an object which is termed as Object Literal.
var person = { };
Javascript dosen't have namespace due to the language design,but we can fake it using Object Literal.
var english = {};
var spanish = {};
english.greet = 'Hello!';
spanish.greet = 'Hola!';
JSON: Javascript Object Notation (Inspired from Object Literals in Javascript)
- JSON is a subset of Object Literal Syntax
- Not all Object Literal Syntax is a JSON, But all JSON is a valid Object Literal Syntax
- JSON has much stricter Syntax Representation
- JSON needs property names to be within doubleQuotes "firstName": "Santhosh"
- Examples:
var objectLiteral = {
firstName: 'Mary',
isAProgrammer: true
}
console.log(JSON.stringify(objectLiteral));
var jsonValue = JSON.parse('{ "firstname": "Mary", "isAProgrammer" : true}'); console.log(jsonValue);
Where something sites physically in the code you write
Example: the variable 'a' sites lexically within the function "hello()"
HINT: 'Lexical' means 'hanving to do with words or grammar'. A Lexical environment exists in programming languages in which where you write something is important. A wrapper to help manage the code that is running
There are lots of lexical environments. Which one is currently running is managed via execution contexts. It can contain things beyond what you've written in your code.
A Name which maps to a unique valueThe name may be defined more than ones, but only can have one value in any given context
That value may be more name/value pairs
Example: Address = "100 Main Street."
Address: { Street: 'Main', Number:100, Apartment: { Floor:3, Number: 301 } } The JS Engine creates a global Execution Context. This Global Execution Context contains the following things:
* Global Object * 'this' * Outer Environment * Your Code * The Global Object is created in memory * 'this' is setup in memory * Outer Environment is created * Setup Memory Space for Variables and Function (AKA Hoisting)
--> During Hoisting the Variables are set to "Undefined" * It runs the code line-by-line One Command is Executed at a time One at a time (and in Order) Where the variable live and how they relate to each other in memory Where a variable is available in your code More than one at a time You don't tell the engine what type of data a variable holds, it figures it out while your code is running.
Variables can hold different types of values because it's all figured out during execution.
Example: Static Typing
bool isNew = 'hello'; //an error
Example: Dynamic Typing
var isNew = true; //no errors
isNew = 'yup!';
isNew = 1;
* UNDEFINED (Represent lack of existence - You shouldn't set a variable to this) * NULL (Represent lack of existence - You can set a variable to this) * BOOLEAN (true or false) * NUMBER (Floating point number - there's always some decimals - Unlike other programming languages, there's only one 'number' type... and it can make math weird) * STRING (a sequence of character - both '' and " " can be used) * SYMBOL (Used in ES6 - the next version of Javascript) A Special Function that syntactically (written differently)- Generally they take two parameters and return one result Operators are functions which uses InFix Notation (+, -, /, *, <, >)
Prefix Notation: +(3, 4);
Postfix Notation: (3 , 4)+;
InFix Notation: (3 + 4); - Javascript uses Infix Notation
Reference: * https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness Converting a value from one type to another
This happens quite often in Javascript as its Dynamic Typed The Coercion within if statements will resolve to Boolean.
Boolean(null) --> false
Boolean(undefined) --> false
Boolean(0) --> false
Conditional Operator mentioned above sets the name variable to '' when the passed in name argument is null (or) undefined (or) 0
console.log(arguments);
The above statement, we print "arguments" supplied to the function
console.log(arguments[0]);
console.log(arguments.length);