A Mutator method is used to control changes to a variable, which is formerly known as getter and setter methods. Several server side languages supports Mutator methods. Lets see how to achieve the same in JavaScript. There are some restrictions to achieve it in obsolete browsers. Lets see the different ways to implement mutator methods in JavaScript, Method 1: Exposing our own getter and setter methods function User(){ this._name = ''; this.getName = function(){ return 'Hey ' + this._name; } this.setName = function(name){ this._name = name } } var u1 = new User(); u1.setName('Ajai') console.log(u1.getName()) //Hey Ajai Method 2: Using ES5 Syntax (supported in all ES5 ready browsers) //with Prototype function User(){ this._name = ''; } User.prototype = { get name(){ return 'Hey ' + this._name; }, set name(name){ this._name = name; } } var u1 = new User(); u1.name = "Ajai" console.log(u1.name) //H...
Javascript tips and tricks, JQuery tips & tricks, JQuery Plugins, CSS Tricks and Tips, things you may not know about javascript, AngularJS tips and tricks, Laravel, PHP tips and tricks, computer programming, grapical user interface using c, c header files, computer graphics lab program, 2d translation, 3d translation, line drawing algorithm, circle drawing algorithm, DDA, Midpoint, 3D projection using c, Personal diary, Mini project using c, Graphical User Interface using C