Example:1 for (var i = 0; i < 5; i++) { (function(x) { setTimeout(function() { console.log(x); }, x * 1000 ); })(i); }
Example:2 var x = 21; var girl = function () { console.log(x); var x = 20; }; girl (); // undefined
Example:3 function x (callBack){ setTimeout(()=>{ const p = "hello" callBack(p) },2000) } x(function(res){ console.log(res)})
Example:4 function cP(successCb,errorCb){ setTimeout(()=>{ let data ="ajay"; if(data){ successCb(data) } else{ errorCb("no Data") } },2000) } cP((res)=>{console.log(res)},(er)=>{console.log(er)})
Example:5 var MySingleTon = (function(){ var instance; var data =[]; var add = function(item){ data.push(item) } var getData = function(){ return data; } var createInstance = function(){ return { Add:add, GetData:getData } } return { getInstance: function(){ return instance || (instance= createInstance()); } } }()); var myModule = MySingleTon.getInstance();
Example:6 function Person(name) { console.log(this) //Window {parent: Window, opener: null, top: Window, length: 4, frames: Window, …} this.name = name; this.sayName = function() { console.log(this.name); }; } Person('Johan') ..................................................... function Person(name) { console.log(this) this.name = name; this.sayName = function() { console.log(this); }; } var person1 = new Person("Nicholas"); var person2 = new Person("Greg"); person1.sayName() // Person {name: "Nicholas", sayName: ƒ}
function x(input){ if(input.includes('_')){ var a = input.split("_"); str = '' for(i = 0; i< a.length; i++){ if(i == 0){ str+=a[i] } else { str+=a[i] && a[i].slice(0,1).toUpperCase(); str+=a[i] && a[i].slice(1) } } } else { var a = input.split(""); str = ''; a.forEach((x)=>{ if(x.toUpperCase() == x){ str +='_'+x.toLowerCase(); } else{ str +=x; } }) } return str; } x("ajayKumaMah")
function x(input){ var a = input.split(""); if(input.includes('_')){ str = ''; var chk = 0; a.forEach((x,i)=>{ if(x == '_'){ chk = 1; } else{ if(chk == 1) { str +=x.toUpperCase(); chk = 0 } else { str +=x } } }) } else { str = ''; a.forEach((x)=>{ if(x.toUpperCase() == x){ str +='_'+x.toLowerCase(); } else{ str +=x; } }) } return str; } x("ajay_kumar_mah")
/Create an object function icreon (){} // constructon 1 var icreon = {} // literal 2 //Object.create Create a new object with the prototype set to a certain object var cat ={ makeSound: function(){ console.log(this.sound) } } var mark = Object.create(cat) mark.sound = 'meyau'; //mark.makeSound(); var dog = Object.create(cat) dog.sound = 'bho Bho'; //dog.makeSound(); function Animal(name) { this.name = name; } Animal.prototype = { canWalk: true, sit: function() { this.canWalk = false console.log(this.name + ' sits down.') } } function ss (a){ Animal.call(this, a); } ss.prototype = Object.create(Animal.prototype); //var a = new Animal('dog'); var b = new ss('hi') //console.log(b.sit()) // es5 function Bird (weight, height){ this.w = weight; this.h = height; } Bird.prototype.Walk = function() { console.log('walk') } function penguin (weight, height){ Bird.call(this, weight, height) } penguin.prototype = Object.create(Bird.prototype); //penguin.prototype.constructor = penguin; penguin.prototype.swim = function(){ console.log('swim') } var a = new penguin(10,20); a.swim(); a.Walk(); //es6 class shap{ constructor(red, green){ this.red = red; this.green = green; } DrawShap (a){ console.log('this is ' + this.red +' '+ a) } } class shapwork extends shap { constructor(a,b){ super(a,b); } workShap (){ console.log(this.red) } } s = new shapwork('red', 'green3') s.DrawShap('Triangle'); let f = new shap(30,40) f.DrawShap('a'); function company(name){ this.name = name; } var locationss = { loc1:"Noida", loc2:"sez" } company.prototype = locationss; var icr = new company('icpl '); console.log(icr.name + icr.loc2) function parent (name){ this.run = 'i m running very fast' this.name = name; } var p = new parent('John'); function child (name){ console.log('hi--' + p.name) parent.call(this, name); } var Child = new child(p.name); console.log(Child.name) function Rectangle (height, width){ this.height = height; this.width = width; } Rectangle.prototype.Area = function(){ this.area = this.height + this.width; console.log(this.area) return this.area; } var react1 = new Rectangle(4,6) console.log(react1.Area())