Skip to main content

Mutator methods in Javascript

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) //Hey Ajai

//without Prototype
var User ={
 _name: '',
 get name(){
  return 'Hey ' + this._name;
 },
 set name(name){
  this._name = name;
 }
}

User.name = "Ajai"
console.log(User.name) //Hey Ajai

var u1 = Object.create(User);
u1.name = "Ajai";
console.log(u1.name) //Hey Ajai


Method 3: Using Object.defineProperty & Object.defineProperties
Object.defineProperty is supported from IE8+ browsers. Sadly Object.defineProperty  in IE8 only accepts DOM Object.
//with Object.defineProperty
var User = {
 _name: ''
}
Object.defineProperty(User, 'name', {
  get: function() {
    return 'Hey ' + this._name;
  },
  set: function(name){
 this._name = name;
  }
});

User.name = 'Ajai';
console.log(User.name); //Hey Ajai

//or

var u1 = Object.create(User);
u1.name = "Ajai";
console.log(u1.name);


Object.defineProperties  is only supported in IE9+ browsers
var User = {
 _name: '',
 _age: 0
}
Object.defineProperties(User, {
  name: {
   get: function() {
  return 'Hey ' + this._name;
   },
   set: function(name){
  this._name = name;
   }
  },
  age: {
   get: function() {
  return 'Im ' + this._age;
   },
   set: function(age){
  this._age = age;
   }
  }
});

User.name = 'Ajai';
User.age = '27';
console.log(User.name); //Hey Ajai
console.log(User.age); // Im 27

In IE8 you can create a fake DOM Element to achieve the same, 
var User = document.createElement('anytag');
document.body.appendChild(User);
User._name = "";
Object.defineProperty(User, 'name', {
  get: function() {
    return 'Hey ' + this._name;
  },
  set: function(name){
 this._name = name;
  }
});

User.name = 'Ajai';
console.log(User.name); //Hey Ajai 


Non standard way :

This is deprecated from web standard. Please avoid this syntax in real-time applications.
var User = {
 _name: ''
};

User.__defineGetter__('name', function(){
 return 'Hey ' + this._name;
});
User.__defineSetter__('name', function(name){
 this._name = name;
});

User.name = 'Ajai';
console.log(User.name); //Hey Ajai


Comments

Post a Comment

Popular posts from this blog

Random Bouncing Ball Animation

Simple animation of random balls that bounces within the screen Source: #include<stdio.h> #include<graphics.h> #include<conio.h> #include<dos.h> #include<math.h> void drawBall(struct ball *b, int color); struct ball{ int x, y; int dx, dy; int radius; }; void main() { int gd=0, gm=VGAHI; int i; struct ball b[10]; initgraph(&gd, &gm, ""); for(i=1;i<=15;i++){ b[i].radius = rand()%20; b[i].x=rand()%getmaxx(); b[i].y=rand()%getmaxy(); b[i].dx=2; b[i].dy=4; } while(!kbhit()) { delay(5); cleardevice(); for(i=1;i<=15;i++) drawBall(&b[i],i); } closegraph(); } void drawBall(struct ball *b, int color){ setfillstyle(1,color); setcolor(color); fillellipse(b->x, b->y, b->radius, b->radius); if(b->x+b->radius > getmaxx() || b->x-b->radius<0) b->dx = -b->dx; if(b->y+b->radius > getmaxy() || b->y-b->radius<0) b->dy = -b->dy; b->x+=b-...

Simple Animation Using C

Here i have used fillpoly function to draw the object body and used fillellipse function to draw tier. animation is done by looping through the objects x & y position until user hits a key. Keypress event is achived  by using kbhit function. Smoothness of animation is controlled by delay function. Change the delay values to change the animation speed Source: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void main() { int gd=DETECT,gm,i=-300,j; int poly[16]={100,100,250,100,250,50,300,50,325,90,325,140,100,140,100,100}; int tpoly[16]={100,100,250,100,250,50,300,50,325,90,325,140,100,140,100,100}; initgraph(&gd,&gm,""); getch(); while(!kbhit()) { for(j=0;j<16;j+=2) { poly[j]=tpoly[j]+i; } fillpoly(8,poly); setfillstyle(5,7); bar(275+i,60,295+i,85); setfillstyle(5,8); fillellipse(140+i,140,20,20); fillellipse(280+i,140,20,20); setfillstyle(1,0); fillellipse(140+i,140,10,10); fillellipse(280+i,140,10...

Things you may not know about CSS

Grammatical error like style using CSS: .grammar{ -moz-text-decoration-color: green; -moz-text-decoration-line: underline; -moz-text-decoration-style: wavy; text-decoration-color: green; text-decoration-line: underline; text-decoration-style: wavy; } Output : CSS 'empty-cells' Property table{ empty-cells:hide; }    CSS @supports  Usually we do CSS feature-test using JavaScript. Now it can be done in CSS with a new property '@supports' . So for Firefox 22+ , Chrome 28+, Opera 12.1+ supports this feature. So we can look forward to using it soon! Example: /* basic usage */ @supports(prop:value) { /* more styles */ } /* real usage */ @supports (display: flex) { div { display: flex; } } /* testing prefixes too */ @supports (display: -webkit-flex) or (display: -moz-flex) or (display: flex) { section { display: -webkit-flex; display: -moz-flex; display: f...