Skip to content

Commit 6c47cf4

Browse files
committed
Javacript exmples initialize
1 parent ed7648d commit 6c47cf4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1810
-0
lines changed

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Current TS File",
9+
"type": "node",
10+
"request": "launch",
11+
"args": ["${workspaceRoot}/test.ts"],
12+
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
13+
"sourceMaps": true,
14+
"cwd": "${workspaceRoot}",
15+
"protocol": "inspector",
16+
}
17+
]
18+
}

arrays.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const list = require('./mock/list');
2+
3+
function removeByFilter(array, index) {
4+
console.log('indes', index);
5+
return array.filter((el, ind) => ind !== index)
6+
}
7+
8+
function removeBySplice(array, index) {
9+
if (index !== -1) {
10+
array.splice(index, 1);
11+
}
12+
return array;
13+
}
14+
15+
/*******************
16+
* Arrays
17+
*/
18+
let names = ['chethan', 'venkatesh', 'babu'];
19+
let num = new Array(1990, 1991, 1993);
20+
console.log(names);
21+
console.log(num.length);
22+
// console.log('removeByFilter', removeByFilter(list, 2));
23+
console.log('removeBySplice', removeBySplice(list, 1));
24+
25+

async-await.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*******************
2+
* async / await
3+
*/
4+
const getIds = new Promise((resolve, reject) => {
5+
setTimeout(() => {
6+
resolve([123, 234, 345, 456]); // sucess then
7+
}, 1500);
8+
});
9+
async function getCallAW() {
10+
const IDS = await getIds;
11+
console.log('11 ------------ ', IDS);
12+
}
13+
getCallAW();
14+
15+
async function getNewCall() {
16+
const IDS = await getIds;
17+
return IDS;
18+
}
19+
getNewCall().then(ID => {
20+
console.log(`20 --------------------- ${ID}`);
21+
});

async.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*********************************
2+
* synchronous
3+
*/
4+
const second = () => {
5+
console.log('Second');
6+
}
7+
8+
const first = () => {
9+
console.log('first');
10+
second();
11+
console.log('end');
12+
}
13+
first();
14+
/***********************************
15+
* asynchronous
16+
*/
17+
console.log('------------------------------------------');
18+
const abc = () => {
19+
setTimeout(() => {
20+
console.log('Async call');
21+
}, 2000);
22+
}
23+
24+
const mnc = () => {
25+
console.log('Starts');
26+
abc();
27+
console.log('ends');
28+
}
29+
mnc();
30+
//-------------------------------------------------------------
31+
function abc1() {
32+
setTimeout(() => {
33+
const q = [123, 234, 345, 456];
34+
console.log('q', q);
35+
setTimeout((id) => {
36+
const res = {
37+
publisher: 'asdfghjkl',
38+
title: 'zxcvbnm'
39+
};
40+
console.log(`${id}--------${res.title}`);
41+
}, 1000, q[2]);
42+
}, 1500);
43+
}
44+
abc1();

bind.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* The bind() method creates a new function that, when called, has its this keyword set to the provided value,
3+
* with a given sequence of arguments preceding any provided when the new function is called.
4+
* -------------------------------------------------------------------------------------------
5+
* bind() returns a bound function that, when executed later,
6+
* will have the correct context ("this") for calling the original function.
7+
* So bind() can be used when the function needs to be called later in certain events when it's useful.
8+
*/
9+
var module = {
10+
x: 42,
11+
getX: function() {
12+
return this.x;
13+
}
14+
}
15+
16+
var unboundGetX = module.getX;
17+
console.log("unboundGetX()", unboundGetX()); // The function gets invoked at the global scope
18+
// expected output: undefined
19+
20+
var boundGetX = unboundGetX.bind(module);
21+
console.log("boundGetX()", boundGetX());
22+
// expected output: 42
23+
// ------------------------------------------------------------------------------------
24+
var obj = {name:"Niladri"};
25+
26+
var greeting = function(a,b,c){
27+
return "welcome "+this.name+" to "+a+" "+b+" in "+c;
28+
};
29+
30+
console.log(greeting.call(obj,"Newtown","KOLKATA","WB"));
31+
// returns output as welcome Niladri to Newtown KOLKATA in WB

call.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* call()/apply() to invoke the function immediately.
3+
* The call() method calls a function with a given this value and arguments provided individually.
4+
*/
5+
6+
function Product(name, price) {
7+
this.name = name;
8+
this.price = price;
9+
}
10+
11+
function Food(name, price) {
12+
Product.call(this, name, price);
13+
this.category = 'food';
14+
}
15+
16+
console.log(new Food('cheese', 5).name);
17+
// expected output: "cheese"

callbacks.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// callback return values
2+
function foo(address, fn){
3+
console.log('called');
4+
fn(`${address} is my name`);
5+
}
6+
7+
foo("chethan", function(value){
8+
console.log(value); // this is where you get the return value
9+
});
10+
11+
// initialContext calling
12+
loadInitScripts(() => {
13+
console.log('script load');
14+
});
15+
16+
// initialContext defining
17+
function loadInitScripts(callback) {
18+
console.log('call back');
19+
callback();
20+
}
21+
22+
// another callback2 sum defining
23+
function sum(callback, a, b) {
24+
console.log('SUM', a + b);
25+
callback(a + b);
26+
}
27+
// calling sum
28+
sum(function disp(value) {
29+
console.log('anvavnabvan', value);
30+
}, 3, 4);

classExmp.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Human {
2+
constructor() {
3+
this.gender = 'FeMale';
4+
}
5+
printGender() {
6+
console.log('gender', this.gender);
7+
}
8+
}
9+
10+
class Person extends Human {
11+
constructor() {
12+
super();
13+
console.log('gender', this.gender)
14+
this.name = 'Chethan';
15+
this.gender = 'Male';
16+
}
17+
18+
printName() {
19+
console.log('name', this.name, this.printGender());
20+
}
21+
}
22+
const person = new Person();
23+
person.printName();
24+
person.printGender();

closure.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function makeAdder(x) {
2+
// parameter `x` is an inner variable
3+
// inner function `add()` uses `x`, so
4+
// it has a "closure" over it
5+
function add(y) {
6+
console.log('y', y);
7+
return y + x;
8+
};
9+
return add;
10+
}
11+
12+
// `plusOne` gets a reference to the inner `add(..)`
13+
// function with closure over the `x` parameter of
14+
// the outer `makeAdder(..)`
15+
var plusOne = makeAdder( 1 );
16+
17+
// `plusTen` gets a reference to the inner `add(..)`
18+
// function with closure over the `x` parameter of
19+
// the outer `makeAdder(..)`
20+
var plusTen = makeAdder( 10 );
21+
console.log(plusOne( 3 ));// 4 <-- 1 + 3
22+
console.log(plusOne( 41 ));// 42 <-- 1 + 41
23+
console.log(plusTen( 13 ));// 23 <-- 10 + 13

common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Common {
2+
months() {
3+
return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
4+
}
5+
}
6+
7+
module.exports = new Common();

0 commit comments

Comments
 (0)