js.l24 code example

Example: js.l24

//	any function that is attaced to an object is called a method



//
//	0 object 
const obj = {
   name: 'mahtab',
   age: 26,
   birthyear: 1996,
   friends: ['jolil', 'kaysar', 'niloy'],
   islisence: true,

   colcAge: function () {
      console.log(this);
      return 2021 - this.birthyear;
   }
};
// console.log(obj.colcAge());
// // console.log(obj['colcAge'] (1996));




//	1 object 
// const obj = {
//    firstName: 'Dulon',
//    lastName: 'Mahadi',
//    birthYear: 1996,
//    job: 'Teacher',
//    friends: ['RAhat', 'Emon', 'Ali'],
//    hasDriverLisence: true,

//    // calcAge: function(birthYear){       // 2.create a function property this object 
//    //    return 2021 - birthYear;
//    // }

//    // calcAge: function(){
//    //    return 2021 - this.birthYear;    // 3.create a function property this object to using "THIS" method 
//    // }

//    calcAge: function(){                   // 1.create a new property in this object
//       this.age = 2021 - this.birthYear;
//       return this.age;
//    }
// };

//    // console.log(obj['calcAge'](1996));  // 2.Bracket[] Notation to call this object function

//       // console.log(obj.calcAge(1996));       // 2.Dot. Notation to call this object function

//          // console.log(obj.calcAge());            // 3.print the function property this object to using "THIS" method 

//             console.log(obj.calcAge());            // 1.create a new property in this object and then print.

//                console.log(obj);                      // 1.create a new property in this object and then print.


//                   console.log(obj.age);                  // 1.create a new property in this object and then print this property




//	2 object 
const obj = {
   firstName: 'Dulon',
   lastName: 'Mahadi',
   birthYear: 1996,
   job: 'Teacher',
   friends: ['RAhat', 'Emon', 'Ali'],
   hasDriverLisence: true,

   calcAge: function(){
      this.age = 2021 - this.birthYear;
      return this.age;
   },

   getSummary: function () {
      console.log(`${obj.firstName} ${obj.lastName} is a ${obj.age}_ years old ${obj.job}. and he has ${obj.hasDriverLisence ? 'a' : 'not a'} Driving lisence.    `);
   }

};
obj.calcAge();
obj.getSummary();



-----------------------------------------------------------------

//
//	print inn object function:

// 	console.log(obj.colcAge());
//	console.log(obj['colcAge'] (1996));

Tags:

Misc Example