Strict Mode:
this feature in javascript that enables a stricter set of rules for writing your js
code. When "use strict" is specified at the begining of a strict mode for that code
block.
'use strict';
/*
let a = 5;
let b = 10;
let c = a + b;
console.log(c);
function add(a,a){
console.log(a+a);
};
add(9,14);
'use strict';
function welcome(name){
message = 'hello';
console.log(message + ' ' + name);
}
welcome('pranesh');
// global strict method:
'use strict';
let x = 15;
console.log(x);
y = 20;
// function strict method:
function myfunc(){
'use strict';
let a = 5;
console.log(a);
b = 10;
}
myfunc();
This keyword:
this keyword in js refers to the context in which a function is called or an object
is accessed. it provides access to the object on which a method is invoked or the
object that is currently being constructed by a cnstructor function.
// implict binding:
const person = {
name: "pranesh",
welcome(){
console.log(`Hello my name is ${this.name}`)
},
}
person.welcome();
// explicit binding:
function sayHello(){
console.log(`hello, ${this.name}`);
}
const person = {name: 'pranesh'};
const person1 = {name: 'dinesh'};
sayHello.call(person);
sayHello.apply(person1);
const welcome = sayHello.bind(person);
welcome();
// arrow functions:
const obj = {
name: "pranesh",
sayHello: () =>{
console.log(`hello, ${this.name}`);
}
};
obj.sayHello();
//constructor function:
function Person(name){
this.name = name;
this.msg = function(){
console.log(`hello, my name is ${this.name}`);
};
}
const person = new Person('pranesh');
person.msg();
// method chaining:
const calculator = {
num1: 0,
add(num){
this.num1 += num; // value = value + num => 0 + 15 = 15
return this;
},
subtract(num){
this.num1 -= num;
return this;
},
multipy(num){
this.num1 *= num;
return this;
},
getResult(){
console.log(`result ${this.num1}`);
},
};
calculator.add(15).getResult();
*/

0 Comments