Skip to content

Commit 2cf8f28

Browse files
committed
Add Object, Scope, and Class tutorials with examples and method implementations
1 parent 55be9cf commit 2cf8f28

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const info = {
2+
Name : "joy",
3+
Age : 21,
4+
City : "New York"
5+
}
6+
7+
console.log(info);
8+
9+
// accessing values
10+
console.log(info.Name);
11+
12+
// access form key
13+
console.log(info["Age"]);
14+
15+
// adding new data
16+
17+
info.Country = "USA";
18+
console.log(info);
19+
20+
// add new function
21+
info.greet = function() {
22+
console.log("welcome to codeswithpankaj");
23+
}
24+
25+
console.log(info);
26+
27+
28+
info.greet();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// create a global variable
2+
3+
var number = 10;
4+
5+
function printNumber() {
6+
7+
// local var
8+
var data = 20;
9+
10+
// access the global variable
11+
console.log("The number is: " + number);
12+
console.log("The data is: " + data);
13+
14+
}
15+
16+
console.log("Accessing global variable outside function: " + number);
17+
// console.log("Accessing local variable outside function: " + data);
18+
19+
function info(){
20+
//console.log("call data inside info(): " + data);
21+
console.log("call number inside info(): " + number);
22+
}
23+
24+
25+
printNumber();
26+
27+
info();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class animal{
2+
3+
color = "white";
4+
5+
info(){
6+
console.log("This is an animal class");
7+
}
8+
9+
type_dog(name){
10+
console.log("Dog name is: " + name);
11+
console.log("Dog type is: dengerous dog");
12+
console.log("Dog color is: "+ this.color);
13+
}
14+
15+
16+
}
17+
18+
19+
// create object
20+
const dog = new animal();
21+
22+
// access method
23+
dog.info();
24+
25+
// send data to method
26+
dog.type_dog("Labrador");
27+
28+
// send value to variable
29+
30+
dog.color = "black";
31+
32+
dog.type_dog("Bulldog");

0 commit comments

Comments
 (0)