1.2. JS Share
1.2. JS Share
1. Primitive
a. String
b. Number
c. Boolean
d. Null
e. Undefined
2. Objects
a. Objects
b. Arrays
c. Functions
3. Loosely type
4. Type of
5. NaN -> type of NaN, NaN === NaN
Loosely type
var a = ‘2’
var b = 2
if(b == a)
Conversion
Array
Var a = [ ];
Var a = [1,2,3];
A.length
Var a = [1, 2 , 3 , “hello world”];
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 referring to the same memory b would still point to memory even when
a points to empty it won't be garbage collected.
A.lentgth = 0
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”
}
Object inside object to create JSON
Var dog = {
“Name”: “golu”,
“Bread”: “pom”,
Owner: {
“Name”: “Siddharth”
}
}
Var dog = {
“Name”: “golu”,
“Bread”: “pom”,
Bark: function() {
console.log(‘Dog barks’);
}
}
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);
Arguments is an array like structure
What is prototype
Dog.prototype.barks = function() {
console.log(‘barks’);
}
d.barks();
__proto__
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
Array.prototype.slice.call(arguments);
function isTwoPassed(){
var args = Array.prototype.slice.call(arguments);
return args.indexOf(2) != -1;
}
isTwoPassed(1,4) //false
isTowPassed(5,3,1,2) //true
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();
var animal = {
eats: true,
walk() {
alert("Animal walk");
}
};
var rabbit = {
__proto__: animal
};
rabbit.walk();
rabbit.walk = function() {
alert("Rabbit! Bounce-bounce!");
};
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){
this.offspring.push(newBaby);
return newBaby;
function Cat(name){
this.name=name;
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
(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);
Inheritance in es6
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
incrementAge() {
return ++this.age;
}