Skip to content

Commit 6482dd8

Browse files
authored
Add files via upload
1 parent 37427c2 commit 6482dd8

6 files changed

+233
-9
lines changed

031-Call-Apply-Bind.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
4+
Function.prototype.call() :
5+
The call() method of Function instances calls this function with a given this value and arguments provided individually.
6+
7+
Function.prototype.apply():
8+
The apply() method of Function instances calls this function with a given this value, and arguments provided as an array (or an array-like object).
9+
10+
Function.prototype.bind()
11+
With the bind() method, an object can borrow a method from another object. The bind() method of Function instances creates a new function that, when called, calls this function with its this keyword set to the provided value, and a given sequence of arguments preceding any provided when the new function is called.
12+
13+
The difference is:
14+
The call() method takes arguments separately.
15+
The apply() method takes arguments as an array.
16+
17+
*/
18+
19+
const car = {
20+
name: 'car',
21+
color: 'black',
22+
getDetails(brand, seats) {
23+
console.log(`This is a ${this.color} ${this.name} of ${brand} company. It has ${seats} seats`);
24+
},
25+
};
26+
27+
const bus = {
28+
name: 'bus',
29+
color: 'blue',
30+
};
31+
32+
33+
car.getDetails('Audi', 5);
34+
console.log('call : ');
35+
car.getDetails.call(bus, 'Star', 50);
36+
console.log('apply : ');
37+
car.getDetails.apply(bus, ['Gold', 30]);
38+
39+
const veh1 = car.getDetails.bind(bus);
40+
veh1('silver', 20);
41+
veh1('bronze', 45);
42+
43+
console.log('-------------------------------------');
44+
45+
const obj = {
46+
animal: "Cats",
47+
sleepDuration: "12 and 16 hours",
48+
};
49+
50+
function greet() {
51+
console.log(this.animal, "typically sleep between", this.sleepDuration);
52+
};
53+
54+
55+
greet.call(obj);
56+
greet.apply(obj);
57+
58+
let gt_fun = greet.bind(obj);
59+
gt_fun();
60+
console.log('-------------------------------------');
61+
62+
63+
function User(displayName) {
64+
this.displayName = displayName;
65+
}
66+
67+
const systemCredentials = {
68+
username: "system",
69+
password: "pass123"
70+
};
71+
User.prototype.login = function (username,password) {
72+
// Implement the code here
73+
if((username === systemCredentials.username) &&
74+
(password === systemCredentials.password)){
75+
console.log('Login successful!');
76+
}else{
77+
console.log('Login failed!');
78+
}
79+
};
80+
81+
// Example usage:
82+
const user = new User("John Doe");
83+
// Create the reusable loginFunction here
84+
let loginFunction = user.login.bind(systemCredentials);
85+
loginFunction("system", "pass123"); // Expected output: "Login successful!"
86+
loginFunction("wrongUsername", "wrongPassword"); // Expected output: "Login

044-Error_Handle.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ try{
1414
1515
}
1616
17-
Here are some of the most common errors in JavaScript:
18-
ReferenceError
19-
SyntaxError : syntax error can not work with try and catch block
20-
TypeError
21-
RangeError
17+
Here are some of the most common errors in JavaScript:
18+
- ReferenceError
19+
- SyntaxError : syntax error can not work with try and catch block
20+
- TypeError
21+
- RangeError
2222
2323
There are some other errors in JavaScript. These other errors include:
24-
AggregateError
25-
Error
26-
InternalError
27-
URIError
24+
- AggregateError
25+
- Error
26+
- InternalError
27+
- URIError
2828
*/
2929

3030
console.log("\n----------Reference Error");

047-SetTimeout-Asynchronous.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
setTimeout: The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires. Also it will start to execute in next line even timeout function not executed.
5+
6+
syntax:
7+
setTimeout(functionRef, delay, param1, param2,....., paramN);
8+
9+
param1, …, paramN : Optional Additional arguments which are passed through to the function specified by functionRef.
10+
11+
DOM API is synchronous in nature.
12+
13+
SetTimeout, FileReader and Geolocation are asynchronous in nature and can be used to delay the function, read file and get the location respectively.
14+
15+
Note : It is used with let and var variable with different way.
16+
17+
*/
18+
19+
console.log('------------var----------------');
20+
for(var i =0; i < 5; i++){
21+
setTimeout(()=>{
22+
console.log(i);
23+
},1000);
24+
}
25+
26+
console.log('------------let----------------');
27+
for(let i =0; i < 5; i++){
28+
setTimeout(()=>{
29+
console.log(i);
30+
},1000);
31+
}
32+
33+
34+
console.log('----------------------------');
35+
36+
const myArray = ["zero", "one", "two"];
37+
38+
39+
function printArray(arr,time,obj){
40+
setTimeout(show, time, arr, time, obj);
41+
};
42+
43+
function show(arr, time, obj){
44+
45+
if(obj === undefined){
46+
obj = arr[0];
47+
}
48+
49+
console.log(obj);
50+
51+
if(obj !== arr[arr.length - 1]){
52+
printArray(arr, time, arr[arr.indexOf(obj)+1]);
53+
}else{
54+
console.log('Finished');
55+
}
56+
};
57+
58+
printArray(myArray,1500);
59+

048-setInterval-and-clearInterval.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
The setInterval() method, offered on the Window and WorkerGlobalScope interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
5+
6+
setInterval() method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval().
7+
8+
The setInterval() :
9+
10+
setInterval(functionRef, delay, param1, param2,....., paramN);
11+
12+
param1, …, paramN : Optional Additional arguments which are passed through to the function specified by functionRef.
13+
14+
15+
clearInterval():
16+
17+
clearInterval(intervalID)
18+
19+
*/
20+
21+
let second = 0;
22+
23+
let timer = setInterval(()=>{
24+
second++;
25+
console.log(second);
26+
27+
if(second >= 5){
28+
clearInterval(timer);
29+
second = 0;
30+
console.log('Finished');
31+
}
32+
},1000);
33+
34+
35+
36+
37+

049-Print_data.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
consol.log(...+....+...+...) not add space among strings automatically
4+
consol.log(...,....,...,...) add space among strings automatically
5+
*/
6+
var str = "Hello";
7+
var year = 2023;
8+
var arr = ['aa','bb','cc','dd'];
9+
console.log(str,'World');
10+
console.log(str,'World',year);
11+
console.log("concate: ", str.concat('World'));
12+
console.log("arr : " + arr);
13+
console.log("(10>3) : " + (10>3));
14+
console.log("(10<3) : " + (10<3));
15+
console.log("'3' + 3 : " + '3' + 3);
16+
console.log("3 + '3' : " + 3 + '3');
17+
console.log("'3' + '3' : " + '3' + '3');
18+
console.log("3 + 3 : " + 3 + 3);
19+
console.log("(3 + 3) : " + (3 + 3));
20+
console.log(3 + 3);
21+
22+
23+
24+
25+
26+
27+

050-Chrome_console.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Author : Jaydatt Patel
3+
* Run this program in Chrome Browser consol
4+
*/
5+
6+
7+
8+
console.log("Hello, World");
9+
console.log("%cHello, World", "color: blue; font-size: 40px");
10+
console.log("Hello " + "there, " + "World");//Not space add automatically
11+
console.log("Hello","there,","World");//space add automatically
12+
13+
alert("Alert Massage Small Popup Window...");//to popup window for alert
14+
confirm("Do you want to continue ?");// conformation window to get true and false
15+
prompt("Enter Value:"); //get input from user

0 commit comments

Comments
 (0)