DEV Community

Cover image for Method js JavaScript core principles
Edeke Emmanuel
Edeke Emmanuel

Posted on

Method js JavaScript core principles

JavaScript Method is a property of an object, that performs certain function or task.

Object Method

It's a function property in an object scope.

// object containing major types of method
const person = {
    name: 'John',

    greet: function() { 
let reply = 'Good morning ' + this.name
console.log(reply); 
},

//short syntax
greetBack() {
  return "How are you " + this.name + " ?"
},

//Compose Method
["reply" + 2]() {
  console.log('I am fine ' + this.name)
},

//Async Method
kate: async function() {
  console.log(this.name + ', Boss want to see you');
},

async boss() {
  let reply = 'Have your seat, ' + this.name
  await console.log(reply);
},

//Generator Method 
increment: function* () {
    let index = 5;
    while (true) {
      yield console.log(index++);
    }
  },

async *decrement () {
    yield console.log(4);
  },

};

person.greet(); //Good morning John
console.log(person.greetBack()); //How are you John ?
person.reply2(); // I am fine John
person.kate(); //John, Boss want to see you
person.boss(); //Have your seat, John 
person.increment().next(); //5
person.decrement().next(); //4 
Enter fullscreen mode Exit fullscreen mode

Adding Method to Object

#1
var a = new Object();
a.method = function(){}

#2
var a = new Object();
a.prototype.method = function(){}

#3
function myObject() {
    this.method = function(){}
}
var a = new myObject();

#4
function myObject() {}
myObject.prototype.method = function(){}
var a = new myObject();

#5
var a = {
    method: function(){}
}
Enter fullscreen mode Exit fullscreen mode

Class Method

class Greeting {
  msg = "Good morning friend's";
//Method
  morningGreet() {
    return this.msg;
  }
}

class Person extends Greeting {
  speak() {
    return super.morningGreet();
  }
}

//instance of class
const john = new Person();
console.log(john.speak());   
Enter fullscreen mode Exit fullscreen mode
class Person { 

constructor(name) {         
this.name = name;   
} 

getName() {     
    return this.name;
    }

//Static Method
static createAnonymous(gender) {
        let name = gender == "female" ? "John Doe" : "Jane Doe";
        let req = new Person(name);
        return console.log(req);
} 

//Private Method
#occupation = "UI/UX";
#biodata; 

#status() {
  let req = "She is an elegant " + this.#occupation + " by profession and well mannered person."
  return console.log(req)
}

//Private Static Method
static #age() {
  let req = 'Jane Doe is 20 year old beautiful woman.'  
  return console.log(req)
}

//returning request of both private and static method with getProfile()
getProfile() {
  let statusA, statusB;
//calling private and static private method in statusA and statusB
  statusA = Person.#age();
  statusB = this.#status();

  let req = statusA + statusB;
  return console.log(req);
}   

}
Person.createAnonymous('female');

//calling instance of static method
let person = new Person();
person.constructor.createAnonymous('female');

person.getProfile();

person.createAnonymous('female') //Type Error
Enter fullscreen mode Exit fullscreen mode

Note:

  • you can't call a private method into a static private method but you can call static private method inside private method
  • If you attempt to call the static method from an instance of the class, you’ll get an error when using new.

Accessing Method

This is a means of getting the method name from the object name.

objectName.methodName()
Enter fullscreen mode Exit fullscreen mode
const client = {
name:  'Jane Doe',
greet() {
return `Good morning Mrs ${client.name}`;
}
}
Enter fullscreen mode Exit fullscreen mode
//Accessing Method
client.greet(); //Good morning Mrs Jane Doe
Enter fullscreen mode Exit fullscreen mode
//Accessing Property
client.name; //Jane Doe 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)