Skip to content

Commit 56c9afd

Browse files
Working on Objects
1 parent 9192fa7 commit 56c9afd

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

Objects/JSConstructorFunction.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// In JavaScript, a constructor function is used to create objects.
2+
3+
function Person (){
4+
this.name= 'Chittaranjan',
5+
this.age= 23
6+
}
7+
8+
const person = new Person()
9+
console.log(person.name, person.age)
10+
11+
// constructor function
12+
function Man(){
13+
this.name = 'Chitta',
14+
this.age = 34,
15+
this.greet = function(){
16+
console.log('Hello '+this.name)
17+
}
18+
}
19+
20+
man1 = new Man()
21+
man2 = new Man()
22+
man2.name = 'Ashok' // Changing the value for an object
23+
man1.greet()
24+
man2.greet()
25+
26+
// JavaScript Constructor Function Parameters
27+
28+
function Person1(name, age, gender){
29+
this.name = name,
30+
this.age = age,
31+
this.gender = gender,
32+
this.intro = function(){
33+
console.log(this.name+' is '+this.age+' years old - '+this.gender)
34+
}
35+
}
36+
37+
p1 = new Person1('Chitta', 34, 'M')
38+
p2 = new Person1('Ambuja', 29, 'F')
39+
p1.intro()
40+
p2.intro()
41+
42+
/* Create Objects: Constructor Function Vs Object Literal
43+
Object Literal is generally used to create a single object.
44+
The constructor function is useful if you want to create multiple objects.
45+
*/
46+
47+
// using object literal
48+
let person2 = {
49+
name: 'Sam'
50+
}
51+
52+
function Person2 () {
53+
this.name = 'Sam'
54+
}
55+
56+
let per1 = new Person2();
57+
let per2 = new Person2();
58+
console.log(per1.name);
59+
console.log(per2.name);
60+
61+
62+
/*JavaScript Object Prototype
63+
You can also add properties and methods to a constructor function using a prototype. */
64+
// constructor function
65+
function Person3 () {
66+
this.name = 'John',
67+
this.age = 23
68+
}
69+
70+
// creating objects
71+
let per31 = new Person3();
72+
let per32 = new Person3();
73+
74+
// adding new property to constructor function
75+
Person3.prototype.gender = 'Male';
76+
77+
console.log(per31.gender); // Male
78+
console.log(per32.gender); // Male
79+
80+
//JavaScript Built-in Constructors
81+
let a = new Object(); // A new Object object
82+
let b = new String(); // A new String object
83+
let c = new Number(); // A new Number object
84+
let d = new Boolean(); // A new Boolean object
85+

Objects/JSMethods.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// creating an object
2+
let stud = { };
3+
4+
// adding a property
5+
stud.name = 'John';
6+
7+
// adding a method
8+
stud.greet = function() {
9+
console.log('hello');
10+
}
11+
12+
// accessing a method
13+
stud.greet(); // hello
14+
15+
// Use of this keyword and methods inside a object
16+
const student = {
17+
name: 'Ambuja',
18+
rollNo: 234556,
19+
totalMarks: 41,
20+
studInfo: function(){
21+
console.log('name of the student - '+this.name)
22+
console.log('Rool No of the student - '+this.rollNo)
23+
},
24+
result: function(){
25+
if(this.totalMarks>=40)
26+
console.log('PASSED')
27+
else console.log('FAILED')
28+
}
29+
}
30+
31+
student.studInfo()
32+
student.result()
33+
34+
// User of the this.variable and local varible
35+
const person = {
36+
name: 'Chittaranjan',
37+
age: 30,
38+
greet: function() {
39+
let surname = 'Swain';
40+
console.log('The name is' + ' ' + this.name + ' ' + surname); }
41+
};
42+
43+
person.greet();

Objects/JSObjects.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const person = {
2+
name: 'chitta',
3+
age: 20
4+
}
5+
6+
console.log(person)
7+
console.log(typeof(person))
8+
console.log(person.name) // Dot notaion
9+
console.log(person['age']) // bracket notation
10+
person.age = 30
11+
console.log(person.age)

Objects/ObjectFunctions.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const person = {
2+
name: 'chitta',
3+
age: 20,
4+
greet: function() {
5+
console.log('Hello')
6+
}
7+
}
8+
9+
person.greet()
10+
11+
const student = {
12+
name: 'Ambuja',
13+
rollNo: 234556,
14+
totalMarks: 41,
15+
studInfo: function(){
16+
console.log('name of the student - '+student.name)
17+
console.log('Rool No of the student - '+student.rollNo)
18+
},
19+
result: function(){
20+
if(student.totalMarks>=40)
21+
console.log('PASSED')
22+
else console.log('FAILED')
23+
}
24+
}
25+
26+
student.studInfo()
27+
student.result()

0 commit comments

Comments
 (0)