Function: Makecounter Var Count
Function: Makecounter Var Count
Closure in Javascript
function makeWorker() {
var name = "Pete";
return function() {
alert(name);
};
}
// create a function
var work = makeWorker();
// call it
work();
function makeCounter() {
var count = 0;
return function() {
return count++; / / has access to the outer "count"
};
}
alert( counter() )
; / / 0
alert( counter() ) ; / / 1
alert( counter() ); // 2
function makeCounter() {
var count = 0;
return function() {
return count++;
};
}
alert( counter1() )
; / / 0
alert( counter1() ) ; / / 1
In other words, a closure gives you access to an outer function's scope from an inner
function. In JavaScript, closures are created every time a function is created, at
function creation time. To use a closure, define a function inside another function and
expose it.
(function(){
console.log(1)
})()
Currying
function add(x,y){
if(arguments.length > 1) {
return x+y;
}else if(arguments.length == 1){
return function (y){
return x+y;
}
}
}
add(2,3);
add(2)(3);
obj.first().second().third();
Inheritance in es6
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
incrementAge() {
return ++this.age;
}
Lecture 2
function abc() {
console.log(a);
}
function abc() {
var a = 10;
console.log(a);
}
Hoisting
var z = 2;
function check() {
console.log(z);
var z = 10;
}
check();
function log(){
var args = Array.prototype.slice.call(arguments);
args.unshift('(app)');
console.log.apply(console, args);
}
log('my message',’hello’);
log('my message', 'your message'); //(app) my message your message
var animal = {
eats: true,
walk: function() {
alert("Animal walk");
}
};
var rabbit = {
jumps: true,
__proto__: animal
};
console.log(rabbit);
var longEar = {
earLength: 10,
__proto__: rabbit
};
console.log(longEar);
rabbit.walk();
rabbit.walk();
var a = {
x: 10,
calculate: function (z) {
return this.x + this.y + z;
}
};
var b = {
y: 20,
__proto__: a
};
var c = {
y: 30,
__proto__: a
};
// a constructor function
function Foo(y) {
// which may create objects
// by specified pattern: they have after
// creation own "y" property
this.y = y;
}
console.log(
);
function Mammal(name){
this.name=name;
this.offspring=[];
Mammal.prototype.haveABaby=function(name){
return newBaby;
function Cat(name){
this.name=name;
Loosely typed
var a = ‘2’
var b = 2
if(b == a)
Conversion
Function in function
var a = function
function try(fn) {
console.log(fn());
}
try(function(){return 8});
try();
Obj.a
obj[‘a’]
Why we do this ?
This is because we would want to get data where we know the name of the property then it
would be possible with
Var z= ‘a’;
Obj[z];
Var dog = {
“Name”: “golu”,
“Bread”: “pom”
}
Var dog = {
“Name”: “golu”,
“Bread”: “pom”,
Owner: {
“Name”: “Siddharth”
}
}
Dog.bark
Var dog = {
“Name”: “golu”,
“Bread”: “pom”,
Bark: function() {
console.log(this.name+” ”+“ barks’);
}
}
Function Dog() {
console.log(a);
console.log(arguments);
}
Dog(1);
What is prototype
Dog.prototype.barks = function() {
console.log(‘barks’);
}
d.barks();
__proto__
Array
Var a = [ ];
Var a = [1,2,3];
A.length
It works because array internally is an object so in object you can have any key value pair.
a[3]() works
Array of functions;
Empty an array
A = [1,2,3];
B = A;
A = [];
Problem is a and b are reffering to same memory b would still point to memory even when a
points to empty it wont be garbage collected.
A.lentgth = 0
null means empty or non-existent value which is used by programmers to indicate “no
value”. null is a primitive value and you can assign null to any variable. null is not an object,
it is a primitive value. For example, you cannot add properties to it. Sometimes people
wrongly assume that it is an object, because typeof null returns "object".
undefined means, value of the variable is not defined. JavaScript has a global variable
undefined whose value is "undefined" and typeof undefined is also "undefined".
Remember, undefined is not a constant or a keyword. undefined is a type with exactly one
value: undefined. Assigning a new value to it does not change the value of the type
undefined.
typeof(null) “object”
typeof(undefined) “undefined”
Null == undefined
typeof(NaN) is number
+’abc’
NaN
Type conversion in ==
Type check in ===
1 == ‘1’ true