Adding Methods in JavaScript Objects
Methods in Objects
functions/methods are stored as object properties. We can easily refer to it by using this keyword. As we already know that this keyword is a referer to the current object, so we can access all its properties using it. Here below is an example where changeMarks() is an Object Method.
Syntax:-
function obj_1(param_1) {
this.param_1= param_1;
this.change_obj_1 = function (param_1) {
this.param_1= param_1;
}
}
var my_custom_obj_1 = new obj_1("cartoon");
console.log(my_custom_obj_1.param_1) // will output cartoon
my_custom_obj_1.change_obj_1("Animations")
console.log(my_custom_obj_1.param_1) // will output Animations